Skip to content
UPEEC

Macro Pitfalls

Macros are text substitution — missing parentheses, double evaluation and swallowed semicolons cause bugs.

Intermediate
Comments 0

#define SQ(x) x*x gives SQ(1+2) = 5. SQ(i++) increments twice. Parenthesise everything and wrap statement macros in do { … } while (0).

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.