🇯🇵 日本語 | 🇺🇸 English

Quiz (Variables & printf)

Interactive quiz on C variables and printf.

Question 1 — Trace the assignments

Walk through the program in your head first, then pick an answer.
#include<stdio.h>

int main(void){
  int a, b;
  a = 10;
  b = 30;
  b = a;
  a = 5;
  printf("a is %d, b is %d \n", a, b);
  return 0;
}

What does this print?

a is 10, b is 30
a is 5, b is 10
a is 5, b is 30
a is 10, b is 10
Explanation: Trace the assignments one by one.
(1) a = 10 → a: 10
(2) b = 30 → b: 30
(3) b = ab takes the current value of a (10) → b: 10 (the 30 is overwritten)
(4) a = 5a is overwritten with 5 → a: 5
a is 5, b is 10

Question 2 — What if you use %f for an int?

Same program as Question 1, but printf's %d has been swapped for %f.
int a, b;
a = 10; b = 30; b = a; a = 5;
printf("a is %f, b is %f \n", a, b); // changed to %f!

What does this print?

a is 5.000000, b is 10.000000
a is 0.000000, b is 0.000000 (or similar garbage)
Compile error
Explanation: Using %f (for floating-point) on an int produces the wrong value.
The exact output depends on the platform, but you'll typically see 0.000000 or some meaningless number.
Use %d for int and %f for double.

Question 3 — What if you use %d for a double?

double a, b;
a = 3.1; b = 2.6; b = 2.12; a = 4.52;
printf("a is %f, b is %d \n", a, b); // only b uses %d!

What does this print?

a is 4.520000, b is 2.120000
a is 4.520000, b is 2
a is 4.520000, b is (garbage value)
Explanation: a prints correctly as 4.520000 with %f, but b is a double being printed with %d (for int), so it won't display correctly.
On some platforms you'll see something like -488420520.
Always use %f for double values.

Question 4 — %.2f and rounding

How does %.2f handle trailing digits?
#include<stdio.h>

int main(void){
  double a = 3.145;
  double b = 3.144;
  printf("a=%.2f, b=%.2f\n", a, b);
  return 0;
}

What does this print?

a=3.14, b=3.14 (truncated)
a=3.14, b=3.15
a=3.15, b=3.14 (rounded)
a=3.145, b=3.144 (as-is)
Explanation: %.2f rounds the value (it doesn't truncate).
3.145 → third decimal is 5 → rounds up → 3.15
3.144 → third decimal is 4 → rounds down → 3.14
This only changes how the value is displayed; the variables a and b themselves are unchanged.

Question 5 — Multiple values in one printf

A single printf can display several variables at once.
#include<stdio.h>

int main(void){
  int age = 25;
  int score = 82;
  printf("age: %d score: %d\n", age, score);
  return 0;
}

What does this print?

age: 25 score: 82
age: %d score: %d
age: 25 score: %d (second %d not replaced)
age: 82 score: 25 (order swapped)
Explanation: Each %d in the format string is filled in in the order of the arguments after the comma.
(1) First %d ← age (=25)
(2) Second %d ← score (=82)
Answer: age: 25 score: 82

Key point: You don't need a separate printf for each variable — one call can print as many values as you like. Just make sure the number of format specifiers matches the number of arguments.

Question 6 — \n in the middle of printf

You can place \n anywhere in the format string to break lines.
#include<stdio.h>

int main(void){
  int x = 3, y = 7;
  printf("x = %d\ny = %d\n", x, y);
  return 0;
}

What does this print?

x = 3 y = 7 (all on one line)
Line 1: x = 3 / Line 2: y = 7 (two lines)
x = 3\ny = 7\n (\n printed as text)
Compile error (only one \n allowed)
Explanation: \n can appear anywhere in the format string and means "go to the next line" at that spot.
Walking through "x = %d\ny = %d\n":
(1) Print x = 3.
(2) \n → newline.
(3) Print y = 7.
(4) Final \n → newline.
Output:
x = 3
y = 7

Key point: A single printf can produce multi-line output by embedding \n in the middle of the string.

Related lessons

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