Input with scanf
Reading from the keyboardscanf reads what the user types. Everything typed — including the Enter key — first goes into an
input buffer, and scanf takes characters from it. In the lab below you watch that buffer character by character:
what is skipped, what is read, where scanf stops — and why the program sometimes does not wait for you.
- &scanf needs the address
- %lfreads a double
- Enterstays in the buffer as \n
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The input buffer
When the user types, the characters do not go straight into your variables. They wait in an input buffer until Enter is pressed — and Enter itself is stored there too, as '\n'. scanf then takes characters from the front of the buffer, one directive of the format at a time. Whatever it does not read stays in the buffer for the next input call.
User types: 21 5.9 and presses Enter
Buffer: 2 1 ␠ 5 . 9 \n
scanf("%d %f", &age, &height);
%d reads 21 → age
' ' skips the space
%f reads 5.9 → height
\n stays in the buffer!
| Specifier | Reads into | Skips whitespace first? | Input → value |
|---|---|---|---|
| %d | int | yes | " 42abc" → 42 (abc stays) |
| %u | unsigned int | yes | "7" → 7 |
| %ld %lld | long / long long | yes | "9000000000" → 9000000000 |
| %x | int (hexadecimal input) | yes | "1f" → 31 |
| %f | float | yes | "2.5" → 2.5 |
| %lf | double | yes | "3.14159" → 3.14159 |
| %c | char | NO — takes the next character | "\n" → '\n' |
| %c | char | yes (because of the space) | "\n y" → 'y' |
| %s | char array (a word) | yes — stops at whitespace | "Asha Rao" → "Asha" |
The basics
- Print a prompt first, so the user knows what to type.
- The format string lists what to read: scanf("%d", &n) reads one int.
- Give the ADDRESS of each variable with &: scanf needs to know where to store the value.
- The types must match: %d → int, %f → float, %lf → double, %c → char.
- scanf returns how many values it stored — check it to catch wrong input.
How whitespace is handled
- %d, %f, %lf and %s skip spaces, tabs and Enters before the value.
- %c does NOT skip whitespace: it takes the very next character, even '\n'.
- A space in the format skips any amount of whitespace — even none.
- Any other character in the format (such as a comma) must be typed exactly.
- A conversion stops at the first character that does not fit; that character stays in the buffer.
Reading safely
- After reading a number, write " %c" (with a space) to read a character.
- Never put \n or text like "Enter:" inside the scanf format.
- If scanf returns less than expected, the bad input is still in the buffer.
- getchar() reads exactly one character (returns an int; EOF at the end of input).
- In the Loops part you will learn to empty the rest of a line: while (getchar() != '\n');
Reading three different values
Every line, explained.
#include <stdio.h>
int main(void)
{
char initial;
int age;
double marks;
A double — so it must be read with %lf.
printf("Initial, age and marks: ");
The prompt. The user types e.g. A 19 87.5 and presses Enter.
int n = scanf(" %c %d %lf", &initial, &age, &marks);
The leading space skips any leftover whitespace before the letter; & gives each variable's address.
printf("Read %d values\n", n);
scanf returned 3 — all three values were stored.
printf("%c is %d and scored %.1f\n", initial, age, marks);
Prints A is 19 and scored 87.5
int ch = getchar();
getchar reads ONE character — the Enter that scanf left in the buffer.
printf("Left over: %d\n", ch);
Prints 10, the code of '\n'.
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Forgetting &
scanf("%d", age);
scanf("%d", &age);
scanf needs the address of age. Without &, it writes to a random place and the program usually crashes. gcc -Wall warns about it.
%f for a double
double price;
scanf("%f", &price);
double price;
scanf("%lf", &price);
In scanf, %f means float and %lf means double. (printf is different: %f prints both.)
Reading a character after a number
scanf("%d", &n);
scanf("%c", &c); // gets '\n'
scanf("%d", &n);
scanf(" %c", &c); // skips it
The Enter after the number stays in the buffer, and %c reads it at once. The space in " %c" skips it.
Text inside the format
scanf("Enter age: %d", &age);
printf("Enter age: ");
scanf("%d", &age);
scanf does not print anything. Text in its format must be TYPED by the user exactly — so it almost always fails.
\n at the end of the format
scanf("%d\n", &n);
scanf("%d", &n);
\n means "skip whitespace", so scanf keeps waiting for a non-whitespace character. The program seems to freeze.
Check your understanding
Answer each question — you will see the explanation straight away.
Why is & needed in scanf("%d", &x); ?
&x is the address of x. scanf writes the number to that address.
Which format reads a double?
In scanf, %lf is for double and %f is for float.
The user types 42abc and presses Enter. What does scanf("%d", &n) store and leave?
%d reads digits and stops at the first character that cannot be part of a number.
What does scanf return?
It returns how many conversions were stored (or EOF at the end of input).
After scanf("%d", &n); which call reads the next letter the user types?
The space skips the leftover Enter, so %c gets the letter.
Input with scanf quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.