We use scanf to get information from the user while the program is running. It helps the program read what the user types.
Using scanf for input
scanf("%format_specifier", &variable);The format_specifier tells scanf what type of data to expect, like %d for integers or %f for decimals.
The & symbol before the variable means 'address of' and is needed so scanf can store the input in the right place.
age.int age; scanf("%d", &age);
price.float price; scanf("%f", &price);
name. No & needed for arrays.char name[20]; scanf("%s", name);
This program asks the user to enter their age, reads it using scanf, and then prints it back.
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("You are %d years old.\n", age); return 0; }
Always use the correct format specifier for the type of data you want to read.
For strings (arrays), do not use & before the variable name.
scanf stops reading a string at the first space, so it reads only one word.
scanf reads user input during program execution.
Use format specifiers like %d, %f, and %s to tell scanf what to expect.
Remember to use & before variables except for arrays when using scanf.
Practice
scanf function in C?Solution
Step 1: Understand the role of
scanfscanfis used to get input from the user while the program runs.Step 2: Differentiate from other functions
Functions likeprintfprint output, butscanfreads input.Final Answer:
To read input from the user during program execution -> Option AQuick Check:
scanf reads input [OK]
- Confusing scanf with printf
- Thinking scanf declares variables
- Assuming scanf performs calculations
num using scanf?Solution
Step 1: Identify the correct format specifier for integer
Use%dto read an integer.Step 2: Use the address operator for variables
Use&numto pass the address of the variable toscanf.Final Answer:
scanf("%d", &num); -> Option DQuick Check:
Integer input needs %d and &variable [OK]
- Omitting & before variable
- Using wrong format specifier like %f or %s
- Passing variable instead of its address
25?
int age;
scanf("%d", &age);
printf("Age: %d", age);Solution
Step 1: Understand input reading
scanf("%d", &age);reads the integer 25 and stores it inage.Step 2: Print the stored value
printf("Age: %d", age);prints the string with the value ofage, which is 25.Final Answer:
Age: 25 -> Option BQuick Check:
scanf stores input, printf prints it [OK]
- Forgetting & in scanf causing runtime error
- Using wrong format specifier in printf
- Expecting scanf to print output
int x;
printf("Enter a number: ");
scanf("%d", x);
printf("You entered %d", x);Solution
Step 1: Check scanf usage
scanf("%d", x);is incorrect because it should use&xto pass the address.Step 2: Confirm other parts are correct
Format specifier is correct, semicolons are present, andxis declared.Final Answer:
Missing & before variable x in scanf -> Option AQuick Check:
scanf needs &variable for input [OK]
- Omitting & causing segmentation fault
- Using wrong format specifier
- Assuming scanf prints output
name[20]. Which of the following scanf statements is correct and safe to avoid buffer overflow?Solution
Step 1: Understand string input with scanf
To read a string intoname, use%swithout & because arrays decay to pointers.Step 2: Prevent buffer overflow
Limit input length to 19 characters using%19sto leave space for the null terminator.Final Answer:
scanf("%19s", name); -> Option CQuick Check:
Limit string input length with %19s [OK]
- Using & with arrays in scanf
- Not limiting input length causing overflow
- Using wrong format specifier for strings
Ready to prove it?
Pass the test and this pattern moves unknown
