Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, March 26, 2020

scanf() Function in C

Scanf() function | welcome2protec.com

In the last tutorial, we already learned about printf() function. Today we will learn to use of scanf() function to take input from the user.

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads input given by the user from the standard input such as keyboards.

Syntax:

/*Format specifier defines the type of data to be printed output on the screen*/
printf("Format Specifier", Variable_Address);

Let's see a simple example that gets input from the user and prints the output on the screen of a given number.

Example 1: Integer Input / Output

#include <stdio.h>  
#include <conio.h> 
void main ()  
{  
    int num;
    clrscr();  // used to clear the screen
    /* Text inside the double quotes will display as it is on the screen.*/
    printf("Enter a number: ");    

    /*scanf() reads input entered by a user from the keyboard,  
    and store that numberin a declared variable num */
    scanf("%d", &num); 
   
    /* Format specifier %d,will replace to a number stored in num variable*/
    printf("Entered number is %d ", num);  
    getch();  //used to take a character form keyboard
 }  

Output:

Enter a number: 8
Entered number is 8

Here, we have used %d format specifier inside the scanf() function to take input from the user. When the user enters an integer, it is stored in the num variable.

Notice, that we have used &num inside scanf(). It is because &num gets the address of num, and the value entered by the user is stored in that address.


Example 2: Integer Input / Output

/* Write a program to print sum of two numbers */
#include <stdio.h> 
#include <conio.h> 
void main ()  
{  
    int num1, num2;
    clrscr();  //used to clear screen

    printf("Enter first number: ");
    /* Format specifier %d, is used for integer type value. */
    scanf("%d", &num1);  

    printf("Enter 2nd number: ");   
    scanf("%d", &num2);    

    printf("Sum of %d and %d = %d", num1, num2, num1 + num2); 
    getch();  //used to take a character form keyboard
 }  

Output

Enter first number: 5
Entered 2nd number: 10
Sum of 5 and 10 = 15

Example 3: float and double Input / Output

#include <stdio.h> 
#include <conio.h>
void main ()  
{  
    float num1;
    double num2;
    clrscr();  //used to clear screen

    printf("Enter first number: "); 
    /* Format specifier %f, is used for float type value. */
    scanf("%f", &num1);  

    printf("Enter 2nd number: "); 
    /* Format specifier %lf, is used for double type value. */
    scanf("%lf", &num2);  
   
    printf("num1 = %f ", num1); 
    printf("num2 = %lf ", num2);  
    getch();  //used to take a character form keyboard.
 }  

Output

Enter first number: 12.53
num1 = 12.523000
Entered 2nd number: 10.20
num2 = 10.200000

We use %f and %lf format specifier for float and double respectively.


Points to Remember:

  • All valid C programs must contain the main() function. The code execution begins from the start of the main() function.
  • The printf() is a library function to send formatted output to the screen. The function prints the string as it is on the screen inside the double quotes.
  • To use printf() and scanf() in our program, we need to include <stdio.h> header file using the #include statement.
  • The return 0; statement inside the main() function is the "Exit status" of the program. It's optional.
  • scanf() and printf() both are predefined functions, not a keywords. it's declaration is in header file #include <stdio.h>











Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us