Input buffer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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!
scanf conversion specifiers
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

  1. Print a prompt first, so the user knows what to type.
  2. The format string lists what to read: scanf("%d", &n) reads one int.
  3. Give the ADDRESS of each variable with &: scanf needs to know where to store the value.
  4. The types must match: %d → int, %f → float, %lf → double, %c → char.
  5. scanf returns how many values it stored — check it to catch wrong input.

How whitespace is handled

  1. %d, %f, %lf and %s skip spaces, tabs and Enters before the value.
  2. %c does NOT skip whitespace: it takes the very next character, even '\n'.
  3. A space in the format skips any amount of whitespace — even none.
  4. Any other character in the format (such as a comma) must be typed exactly.
  5. A conversion stops at the first character that does not fit; that character stays in the buffer.

Reading safely

  1. After reading a number, write " %c" (with a space) to read a character.
  2. Never put \n or text like "Enter:" inside the scanf format.
  3. If scanf returns less than expected, the bad input is still in the buffer.
  4. getchar() reads exactly one character (returns an int; EOF at the end of input).
  5. In the Loops part you will learn to empty the rest of a line: while (getchar() != '\n');
Walkthrough

Reading three different values

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 char initial;
6 int age;
7 double marks; A double — so it must be read with %lf.
8 printf("Initial, age and marks: "); The prompt. The user types e.g. A 19 87.5 and presses Enter.
9 int n = scanf(" %c %d %lf", &initial, &age, &marks); The leading space skips any leftover whitespace before the letter; & gives each variable's address.
10 printf("Read %d values\n", n); scanf returned 3 — all three values were stored.
11 printf("%c is %d and scored %.1f\n", initial, age, marks); Prints A is 19 and scored 87.5
12 int ch = getchar(); getchar reads ONE character — the Enter that scanf left in the buffer.
13 printf("Left over: %d\n", ch); Prints 10, the code of '\n'.
14 return 0;
15 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Forgetting &

✗ Wrong
scanf("%d", age);
✓ Right
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

✗ Wrong
double price;
scanf("%f", &price);
✓ Right
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

✗ Wrong
scanf("%d", &n);
scanf("%c", &c);   // gets '\n'
✓ Right
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

✗ Wrong
scanf("Enter age: %d", &age);
✓ Right
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

✗ Wrong
scanf("%d\n", &n);
✓ Right
scanf("%d", &n);

\n means "skip whitespace", so scanf keeps waiting for a non-whitespace character. The program seems to freeze.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

Why is & needed in scanf("%d", &x); ?

Question 2

Which format reads a double?

Question 3

The user types 42abc and presses Enter. What does scanf("%d", &n) store and leave?

Question 4

What does scanf return?

Question 5

After scanf("%d", &n); which call reads the next letter the user types?

Assessment

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.