Difficulty: Easy
Correct Answer: 3
Explanation:
Introduction / Context:
This problem reinforces operator precedence with integer division and macro expansion. The key is correctly computing each argument to the macro before the comparison and understanding that integer division truncates toward zero in C for positive operands.
Given Data / Assumptions:
x = 3
, y = 4
.y/2
with integers equals 2.#define MIN(x, y) (x < y) ? x : y
.
Concept / Approach:
Compute each argument: left argument is x + y/2
which equals 3 + 2 = 5
. Right argument is y - 1 = 3
. The macro compares 5 and 3, selects the smaller (3), and assigns it to z
. The if
prints z
since it is greater than 0.
Step-by-Step Solution:
y/2
→ 2.Left argument: x + 2 = 5
.Right argument: y - 1 = 3
.Compare: 5 < 3
is false → choose 3.Print: 3
.
Verification / Alternative check:
Change y
to 5 and re-test: y/2 = 2
(integer), left becomes 5, right becomes 4, still selects 4.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting integer truncation; misreading operator precedence (division before addition and subtraction).
Final Answer:
3.
Discussion & Comments