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:
#define MAX(a, b) (a > b ? a : b)
3+2
and 2+7
.
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:
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:
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.
Discussion & Comments