Question 12
Back to Exercise
Why are multi-statement macros often written as do { … } while (0)?
- A To make the macro run twice
- B So the macro behaves like a single statement and is safe after an if without braces Correct
- C To make it faster
- D Because macros cannot contain braces otherwise
Correct Answer: Option B
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.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!