LearnFrenzy
Sign in Take a free test

C Loops · Loops MCQ · Question 12

Why do C macros use do { … } while (0)? Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links