- Asked 4/16
- 2071 Shrawan · 1 mark
- 2071 Chaitra · 1 mark
- 2072 Chaitra · 1 mark
- 2067 Asar · 2 marks
Syntax and example of scanf()
Short answer
scanf() reads formatted input from stdin. Syntax: scanf("format string", &var1, &var2, ...); with format specifiers like %d. Example: scanf("%d %f %c", &roll, &marks, &grade); reads an integer, float, and character, then printf displays them.
Write syntax and example of scanf().
scanf() reads formatted input from the keyboard (standard input) and stores it in variables. It is declared in <stdio.h>.
Syntax: scanf("format specifiers", &var1, &var2, ...);
- The format string says the type of each input (
%dint,%ffloat,%cchar,%sstring). - The address operator
&is required for ordinary variables (not for strings). - It returns the number of values successfully read.
Example:
#include <stdio.h>
int main(void)
{
int roll;
float marks;
char grade;
scanf("%d %f %c", &roll, &marks, &grade);
printf("Roll=%d Marks=%.1f Grade=%c\n", roll, marks, grade);
return 0;
}
For input 7 85.5 A, output:
Roll=7 Marks=85.5 Grade=A
✓ Answer checked by Claude Opus 5.5 (high effort)