🇯🇵 日本語 | 🇺🇸 English

Quiz (scanf)

Interactive quiz on C scanf.

Question 1 — scanf basics

int a;
double b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%lf", &b);
printf("a is %d, b is %f \n", a, b);
Suppose you enter 5 for a and 3 for b. What gets printed?

What does this print?

a is 5, b is 3.000000
a is 5, b is 3
a is 5.000000, b is 3.000000
Explanation: a is an int printed with %d5. b is a double printed with %f3.000000 (%f defaults to 6 decimal places). Both format specifiers match their types, so the output is correct.

Question 2 — What if you forget &?

scanf("%d", a);   // missing &!
scanf("%lf", b);  // missing &!

What happens?

Runs normally
Crashes as soon as input is received
Prints 0
Explanation: scanf needs the address of the variable (prefixed with &). Without the &, scanf treats the variable's value as if it were an address and writes there, causing invalid memory access and most likely a crash.
For scalar types, always prefix scanf arguments with &.

Question 3 — Wrong type in scanf

An int a is read with %lf, and a double b is read with %d.
int a;
double b;
scanf("%lf", &a);  // %lf for int!
scanf("%d", &b);   // %d for double!
If you enter 3 for a and 5 for b, what does printf("a is %d, b is %f", a, b) output?

What does this print?

a is 3, b is 5.000000
a is 0, b is 0.000000 (no correct value)
Compile error
Explanation: When scanf's format doesn't match the target variable's type, it writes the wrong number of bytes and the variable ends up with an incorrect value. The exact output is platform-dependent, but it won't be what you expect.
In scanf, use %d for integers and %lf for doubles.

Question 4 — Reading two values at once

A pattern where one scanf call reads multiple variables. Note the space between the format specifiers.
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("a=%d, b=%d\n", a, b);
If the user types 3 7 (with a space in between) and presses Enter, what does it print?

What does this print?

a=3, b=7
a=3, b=0 (b is not set)
a=37, b=uninitialized (values concatenated)
Compile error (scanf takes only one variable)
Explanation: A single scanf call can read multiple variables. The format "%d %d" reads two integers separated by whitespace (space, tab, or newline).
· First %d ← first integer 3 → a
· Second %d ← next integer 7 → b
Whitespace in the format string means "skip any amount of whitespace", so 3 7, 3   7 (many spaces), or even input split across lines all work.

Question 5 — Using %f on a double?

In printf, %f works for a double. Does the same apply in scanf?
double x;
printf("Enter a number: ");
scanf("%f", &x);   // %f instead of %lf!
printf("x = %f\n", x);
If you type 3.14 and press Enter, what does it print?

What does this print?

x = 3.140000 (works fine)
x = 0.000000 or a garbage number
Compile error
Explanation: This is a gotcha specific to scanf. printf accepts %f for a double, but scanf requires %lf for a double.
With %f, scanf writes 4 bytes (assuming a float) to the given address, which only partially fills the 8-byte double → the value is garbage.
Remember: for printf, %f is fine; for scanf, you must use %lf.

Question 6 — Extra characters in the format string?

Adding extra characters to the format string (treating it like a prompt) leads to surprising behavior.
int a;
scanf("a=%d", &a);   // "a=" included in the format
printf("a = %d\n", a);
What happens if the user just types 5?

What happens?

Prints "a = 5"
The program doesn't consume the input, and a is not assigned
"a=" is printed and then it waits for input
Compile error
Explanation: Any literal characters in scanf's format string mean that the input must begin with those exact characters.
"a=%d" says "the input starts with a=, followed by an integer". So a=5 works, but a bare 5 doesn't match the leading a=, and scanf fails to read, leaving a unassigned.
To show a prompt, print it with a separate printf. Keep scanf's format string to format specifiers like %d and %lf.

Question 7 — Reading an int and a double in sequence

A common pattern where scanf is called twice to read different types.
int age;
double height;
printf("Age: ");
scanf("%d", &age);
printf("Height (cm): ");
scanf("%lf", &height);
printf("%d years, %.1fcm\n", age, height);
If the user types 25 (Enter) then 170.5 (Enter), what is the output?

What does this print?

25 years, 170.5cm
25 years, 170.500000cm
Error (stops at the second input)
25 years, 0.0cm (second value not read)
Explanation: As long as you pair each type with the right format specifier (%d with &int, %lf with &double), you can call scanf back-to-back as many times as you need. Newlines between inputs are handled automatically.
The final printf uses %.1f, so the output is 170.5 (rounded to one decimal place), not 170.500000.
So the result is 25 years, 170.5cm.

Related lessons

Introduction
printf & scanf
How to use printf and scanf in C. Format specifier reference.
Introduction
Quiz (Variables & printf)
Interactive quiz on C variables and printf.
Introduction
Variables
What are C variables? Visual guide to int, double, and char.
← Previous lesson
Lesson 6: Quiz (Variables & printf)
Next lesson →
Lesson 8: Arithmetic Operators