Given the macro #define SQ(x) x * x, what does printf(“%d”, SQ(2 + 3)); print in C?
Correct answer C. 11
Explanation
Macros substitute text before compilation, so SQ(2 + 3) becomes 2 + 3 * 2 + 3. Multiplication binds first, giving 2 + 6 + 3 = 11, not 25. Writing the macro as #define SQ(x) ((x) * (x)) fixes this, although an argument with side effects such as SQ(i++) would still be evaluated twice. An inline function avoids both problems.
Answer checked How we write and check questions
Discussion