Scanf() :
- This function is usually used as an input statement.
- The format string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s). - The argument list contains a list of variables each preceded by the address list and separated by comma.
- The number of argument is not fixed; however corresponding to each argument there should be a format specifier. Inside the format string the number of argument should tally with the number of format specifier.
Syntax :
scanf(“format string”, argument list);
C – SCANF() EXAMPLE PROGRAM :
if i is an integer and j is a floating point number, to input these two numbers we may use
scanf(“%d%f”, &i, &j);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<conio.h> int main() { char a; int b; float c; double d; clrscr(); printf(“Enter a character \n”); scanf(“%c”,&a); printf(“Enter an Integer \n”); scanf(“%d”,&b); printf(“Enter a SP float no: \n”); scanf(“%f”,&c); printf(“Enter a DP float no : \n”); scanf(“%if”,&d); getch(); return 0; } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter a character X Enter an integer 10 Enter a SP float no 3.140000 Enter a DP float no 3.1400000e+10 |