Macro Pitfalls
Macros are text substitution — missing parentheses, double evaluation and swallowed semicolons cause bugs.
Intermediate
#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.