With int n = 3;, how many times does while (n--) { … } run, and what is n afterwards?
The test uses the old value and then decrements: 3, 2, 1 are true (3 passes); the test with 0 is false but still decrements, leaving n at −1.
C Loops · Exercise 1 of 4
Twelve multiple-choice questions on for, while and do-while, break and continue, loop scope, unsigned and floating-point counters, and nested-loop complexity. Pick an option to check your answer instantly.
With int n = 3;, how many times does while (n--) { … } run, and what is n afterwards?
The test uses the old value and then decrements: 3, 2, 1 are true (3 passes); the test with 0 is false but still decrements, leaving n at −1.
Why are multi-statement macros often written as do { … } while (0)?
A macro expanding to { a; b; } followed by the caller's semicolon breaks an if … else. Wrapping it in do { … } while (0) makes it one statement that requires a semicolon; the loop runs exactly once and is optimised away.
Timed online tests with instant scores and explanations.