Macro argument expressions and operator precedence: what will this program print? #include<stdio.h> #define MAX(a, b) (a > b ? a : b) int main() { int x; x = MAX(3+2, 2+7); printf("%d\n", x); return 0; }

Difficulty: Easy

Correct Answer: 9

Explanation:


Introduction / Context:
This item validates your comprehension of macro substitution when arguments are expressions and how the conditional operator selects the larger of two values. It also reinforces that arithmetic inside macro arguments is evaluated before the comparison inside the expanded macro.


Given Data / Assumptions:

  • Macro: #define MAX(a, b) (a > b ? a : b)
  • Arguments: 3+2 and 2+7.
  • Standard integer arithmetic and operator precedence.


Concept / Approach:
After macro expansion, the program compares the two computed values and takes the larger one. Parentheses around the conditional expression safeguard precedence, so the comparison is between the full expressions, not partial terms.


Step-by-Step Solution:

Compute left: 3+2 = 5.Compute right: 2+7 = 9.Compare: 5 > 9 is false → choose right.Assign: x = 9, and print 9.


Verification / Alternative check:
Replace arguments with values that tie (e.g., 5 and 5) to see either branch chosen equivalently; or add parentheses inside macro definition for defensive coding.


Why Other Options Are Wrong:

Other numeric options do not match the larger expression value.“Compilation error” is incorrect; expressions are valid macro arguments.


Common Pitfalls:
Worrying about precedence because the macro lacks extra parentheses around a and b; here, with simple sums, the result remains correct.


Final Answer:
9.

More Questions from C Preprocessor

Discussion & Comments

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