scanf() function C
In C programming language, scanf() function is used to read character, string, numeric data from keyboard.
Consider below example program where user enters a character. This value is assigned to the variable "ch" and then displayed.
Then, user enters a string and this value is assigned to the variable "str" and then displayed.
Exmaple of printf() AND scanf() function in C
printf("Enter any character \n");
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
printf("Entered string is %s \n", str);
Enter any character
Entered character is D
Enter any string ( upto 100 character )
Durgesh Kumar Tiwari
Entered string is Durgesh
The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string.
Ampersand is used before variable name ?ch? in scanf() statement as &ch.
Key Points to remember in C PRINTF() AND SCANF():
printf() is used to display the output and scanf() is used to read the inputs.
printf() and scanf() functions are declared in â??stdio.hâ?? header file in C library.
All syntax in C language including printf() and scanf() functions are case sensitive.