STEP 2 Review: Arithmetic, Assignment, and Increment

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 2 in these tables .

🧮 Arithmetic operators

Symbol Meaning Example Result
+Addition 3 + 25
-Subtraction 3 - 21
*Multiplication 3 * 26
/Division 7 / 23
%Remainder 7 % 21
  • Precedence: * / % before + and -
  • Parentheses ( ) makes the order explicit

⚠️ Integer ÷ integer = integer (fraction discarded)

printf("%d", 7 / 2);       // 3 (fraction discarded) 
printf("%f", 7.0 / 2);     // 3.500000
printf("%f", (double)7 / 2); // 3.500000 (cast) 
  • Make either operand floating-point to perform floating-point division
  • cast (double)x explicitly converts the type

🔄 Compound assignment

Shorthand Expanded form
a += 5a = a + 5
a -= 5a = a - 5
a *= 5a = a * 5
a /= 5a = a / 5
a %= 5a = a % 5
  • update the variable itself : a common pattern

⏫ Increment and decrement

int a = 5;
a++;   // a = 6 (add 1 to a) 
a--;   // a = 5 (subtract 1 from a) 

// Prefix versus postfix (different inside expressions) 
int x = 5, y;
y = x++; // y=5, x=6 (increase x after using its old value for y) 
y = ++x; // y=7, x=7 (increase x first, then use its new value for y) 
  • As standalone statements a++;, prefix and postfix have the same effect
  • Inside an expression , their values differ

🐞 Basic printf debugging

int a = 10, b = 3;
printf("a=%d b=%d\n", a, b);
int avg = (a + b) / 2;
printf("avg=%d\n", avg); // Expected:6.5 Actual:6 — integer division! 
  • When a result differs from your expectation, print the values just before that operation
  • Print both variable names and values to make the trace easier to follow

② Six code-reading questions

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

Q1. Integer division

printf("%d\n", 17 / 5);

What is the output?

3.4
3
4 (rounding)
3.400000
Explanation: Both operands are integers, so 17 / 5 gives 3: the fractional part is discarded, not rounded to nearest. Use 17.0 / 5 or (double)17 / 5 for floating-point division.

Q2. Floating-point division with a cast

int a = 17, b = 5;
printf("%f\n", (double)a / b);

What is the output?

3.000000
3.400000
3
Compilation error
Explanation: The cast converts a to double before division. 17.0 / 5 is 3.4, printed as 3.400000 with %f. The expression (double)a / b means ((double)a) / b.

Q3. Testing with remainder (%)

int n = 17;
printf("%d %d\n", n / 5, n % 5);

What is the output?

3 2
3.4 0
3 5
2 3
Explanation: 17 / 5 is the integer quotient 3, and 17 % 5 is the remainder 2, satisfying 17 = 5 × 3 + 2. Remainder is useful for divisibility tests such as n % 2 == 0.

Q4. Compound assignment

int a = 10;
a += 5;
a *= 2;
printf("%d\n", a);

What is the output?

25
30
20
15
Explanation: Initially a=10. a += 5 makes it 15; a *= 2 makes it 30. The output is 30.

Q5. Postfix increment

int x = 5;
int y = x++;
printf("x=%d y=%d\n", x, y);

What is the output?

x=5 y=6
x=6 y=5
x=6 y=6
x=5 y=5
Explanation: Postfix x++ supplies the old value 5 to y, then increments x to 6. The output is 5 6. Prefix ++x would increment first, making both values 6.

Q6. Operator precedence

printf("%d\n", 2 + 3 * 4);

What is the output?

20
14
9
24
Explanation: Multiplication has higher precedence than addition. First compute 3 × 4 = 12, then 2 + 12 = 14. To obtain 20, write (2 + 3) * 4.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    Lost the fractional part in integer division
    7 / 2is 3. To retain the fraction, use 7.0 / 2or (double)x / y to cast an operand.
  2. 2
    Computed an average with integer arithmetic
    (a + b) / 2 gives an integer result when a and b are int. (double)(a + b) / 2 uses a cast to change this.
  3. 3
    Prefix ++xand postfix x++were confused
    Their effect is the same as standalone statements, but inside expressions the timing changes the value used. Split operations into separate statements when unsure.
  4. 4
    Misread operator precedence
    2 + 3 * 4is 14 (* first). Use explicit parentheses to avoid mistakes.
  5. 5
    Confused assignment =with comparison ==were confused
    In an if condition, if (x = 5) assigns 5 to x and evaluates as true because 5 is nonzero. For comparison, use ==.
  6. 6
    Reversed the order of printf arguments
    printf("a=%d b=%d", b, a) illustrates a mismatch between the intended output order and the argument order.
  7. 7
    Left printf debugging statements in the program
    Debug output can contaminate the program's intended output. Check before committing .

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Lesson 8: Arithmetic operators Review integer division, casts, and precedence Lesson 9: Arithmetic quiz Check understanding with related questions Assignment & Increment Review += and ++ Lesson 11: Trace variables with printf Find the cause of unexpected results Cheat sheet Review operator precedence on one page