In C programming, consider the following macro and program. What would be the output? #define SQR(x) (x * x) int main() { int a, b = 3; a = SQR(b + 2); printf("%d", a); } Assume standard operator precedence and that printf is correctly declared.

Difficulty: Medium

Correct Answer: 11

Explanation:


Introduction / Context:
This question tests understanding of C preprocessor macros, operator precedence, and the importance of using parentheses in macro definitions. A common mistake is to assume that a macro like SQR(x) will behave exactly like a function, but in reality the macro body is substituted textually before compilation. If the macro is not carefully written, expressions passed as arguments can be expanded in surprising ways, changing the result.



Given Data / Assumptions:

  • The macro is defined as #define SQR(x) (x * x).
  • The variable b is initialized to 3.
  • The expression a = SQR(b + 2); is evaluated using normal C operator precedence.
  • printf is assumed to be correctly declared so that the program compiles.


Concept / Approach:
The preprocessor replaces the macro call SQR(b + 2) with the macro body, substituting x by the actual argument b + 2. Because the macro body is (x * x), the expansion becomes (b + 2 * b + 2), not (b + 2) * (b + 2). This happens because x is replaced textually, and the inner x is not surrounded by its own parentheses. Then the compiler evaluates the resulting expression using operator precedence rules, where multiplication has higher precedence than addition.



Step-by-Step Solution:
Step 1: Expand the macro call: SQR(b + 2) becomes (b + 2 * b + 2).Step 2: Apply operator precedence: multiplication is evaluated before addition, so 2 * b is computed first.Step 3: Substitute the value b = 3, giving (3 + 2 * 3 + 2).Step 4: Compute 2 * 3 = 6, so the expression becomes (3 + 6 + 2).Step 5: Add the terms: 3 + 6 + 2 = 11, so a receives the value 11, and printf prints 11.


Verification / Alternative check:
If the macro had been written correctly as #define SQR(x) ((x) * (x)), the expansion of SQR(b + 2) would have been ((b + 2) * (b + 2)), which equals (5 * 5) = 25. The fact that the actual program produces 11 instead of 25 confirms that the missing parentheses around x in the macro body change the evaluation order and lead to an unexpected result.



Why Other Options Are Wrong:
Option B (25) would be correct only for a properly parenthesized macro ((x) * (x)), which is not what is given. Option C is incorrect because the macro definition is syntactically valid C, so it does not cause a compilation error. Option D is wrong because the behavior is well defined; the program deterministically evaluates the arithmetic expression and prints 11.



Common Pitfalls:
A classic pitfall when writing macros is forgetting to enclose parameters and the entire macro body in parentheses. This can lead to subtle bugs when macros are used with composite expressions. Another mistake is treating macros as if they were type safe functions, when they are actually simple text substitutions. To avoid such issues, it is recommended to write macros like #define SQR(x) ((x) * (x)) or to use inline functions in modern C.



Final Answer:
The program prints 11 as the output.


More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion