STEP 1 Review: Variables, printf, and scanf

Review the key points with a cheat sheet, six code-reading questions, and a checklist of common mistakes.

① Key-point cheat sheet

Review the essentials of STEP 1 in these tables .

🦴 Program structure

#include <stdio.h>

int main(void) {
    printf("Hello\n");
    return 0;
}
  • #include <stdio.h>— declares printf and scanf
  • int main(void)— execution begins here
  • return 0;— signals successful completion

📦 Types and format specifiers

Type Example printfscanf
int42%d%d
double3.14%f%lf
  • For double, printf uses %f; scanf uses %lf(take care!)
  • Use %d for integers and %f for real numbers; do not mix them.
  • Specify precision or width with %.2f(two decimal places) %5d(right-aligned in five columns), for example.

🖨️ Using printf

printf("x=%d, y=%d\n", x, y);
printf("%.2f", 3.146); // → 3.15 (rounded) 
printf("%5d", 42);   // → 42 (right-aligned in five columns) 
printf("%05d", 42);  // → 00042 (zero-padded) 
  • Each conversion such as %d or %f in the format string requires a corresponding argument, in order.
  • \nadds a newline; without it, output continues on the same line.

⌨️ Rules for scanf

int n;
scanf("%d", &n);   // & is required! 

double x;
scanf("%lf", &x); // Use %lf for double 
  • Pass the variable's address (&) ; omitting & can cause a crash.
  • For double, scanf uses %lf(unlike printf's %f).

🔁 Assignments overwrite values in order

int a = 10;  // a:10
int b = 30;  // b:30
b = a;        // b:10 (30 is overwritten) 
a = 5;        // a:5 (10 is overwritten) 
// Result: a=5, b=10 
  • = means assign the right-hand value to the left-hand variable, not equality.
  • A later assignment replaces the previous value

② Six code-reading questions

Execute the code mentally before choosing an answer. Read the explanation even if you get it wrong.

Q1. Assignment order

int a = 10, b = 30;
b = a;
a = 5;
printf("%d %d\n", a, b);

What is the output?

10 30
5 10
5 30
10 10
Explanation: Trace the statements in order: a=10 and b=30 initially; b=a copies 10 into b; a=5 then changes only a. The output is 5 10.

Q2. Printing an integer with %f

int x = 7;
printf("%f\n", x);

What is the output?

7
7.000000
Undefined behavior (often 0.000000 or meaningless output)
Compilation error
Explanation: %f expects a double argument, but n is int. This mismatch causes undefined behavior, so no particular output is guaranteed. A compiler may warn without rejecting the program. Use %d for int and %f for double.

Q3. Rounding with %.2f

double a = 3.146, b = 3.144;
printf("%.2f %.2f\n", a, b);

What is the output?

3.14 3.14 (truncation)
3.14 3.14 (rounding, with a rounded down too)
3.15 3.14 (rounding)
3.146 3.144 (unchanged)
Explanation: %.2f prints two decimal places with rounding. 3.146 rounds to 3.15, while 3.144 rounds to 3.14. Floating-point approximations and the rounding mode can matter at exact halfway boundaries; these examples avoid that boundary.

Q4. What happens without \n?

printf("x=%d", 3);
printf("y=%d\n", 7);

What is the output?

x=3y=7
x=3
y=7
x=3 y=7(automatic space)
Compilation error
Explanation: printf adds neither spaces nor newlines automatically. The first call prints x=3 and the second immediately appends y=7, producing x=3y=7. Include \n wherever a newline is required.

Q5. What happens if scanf has no &?

int n;
scanf("%d", n); // & is missing 
printf("%d\n", n);

What happens to this program?

Works correctly (the compiler inserts &)
Compilation always stops with an error
It may compile, but at runtime it can crash (with a segmentation fault)
The input becomes 0 instead of being stored in n
Explanation: scanf needs the destination variable's address. Omitting & passes an uninitialized int instead of a valid pointer, causing undefined behavior and potentially a segmentation fault. A warning does not necessarily prevent compilation. Use scanf("%d", &n).

Q6. Reading a double with scanf

double x;
scanf("???", &x);
printf("%f\n", x);

Which format belongs in "???"?

%f (the same as printf)
%lf
%d
%lld
Explanation: Use %lf (lowercase L, then f) to read a double with scanf. printf accepts %f and %lf for double, but scanf requires %f for float* and %lf for double*.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    Used %f for int or %d for double
    Even if compilation succeeds, the output is unreliable . Remember each type and its format specifier as a pair .
  2. 2
    Forgot & in scanf
    Use scanf("%d", &n), not scanf("%d", n). scanf needs an address. A character array used as a string does not need an extra &.
  3. 3
    Read a double with scanf using %f
    printf uses %f, but scanf uses %lf. Their formats are asymmetric for double.
  4. 4
    Forgot \n, so output continued on the same line
    printf There is no automatic newline . Include \n where needed.
  5. 5
    Semicolon ;missing
    If the compiler reports a missing ;, check one or two lines before the reported location.
  6. 6
    Incorrect number or order of printf arguments
    "x=%d, y=%d"requires the following arguments in the order x, then y . Reversing them reverses the displayed values.
  7. 7
    Confused assignment =with comparison ==operators
    a = 5means store 5 in a; a == 5asks whether a equals 5.

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Lesson 1: What is programming? When you are unsure what a program does Lesson 2: Hello World When include, main, and return need clarification Lesson 3: Variables When types and assignment overwrites need review Lesson 4: printf When %d, %f, %.2f, and \n need review Lesson 5: scanf Review & and %lf Cheat sheet Review types and format specifiers on one page