Thursday, 27 October 2011

Episode 4: The scanf() Function


The scanf() Function

In our previous example program, we declared the value of the variable. In many cases, we need to input the value of variable from the user. Now we discuss about that how to take input from the user when the program will be run.
For this work, we need a function and that is scanf(). This function take the input from the user when the program is running.The scanf() function is defined in the header file stdio.h.

The structure of the scanf() function :

scanf(“format specifier”,& variable);
Format specifier indicate the what type of data is input and & define the address of the variable. It must be end with a semicolon.


#include<stdio.h>
void main()
{
int a;
float b;
char c;
printf(“Input an interger number a :\n ”);
scanf(“%d”,&a);
printf(“Input an floating number b :\n ”);
scanf(“%f”,&b);
printf(“Input an charecter : \n”);
scanf(“%c”,&c);
}
Output :
Input an interger number a :
100
Input an floating number b :
5.50
Input an charecter :
S


We also take input of various types of data to use one scanf() function. Like as,
scan(“%d %f %c”, &a, &b, &c);

So dear friends, I hope you are now understand the basic concept of scanf() function.

Written by “Shojib”

No comments:

Post a Comment