Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.