Follow macro expansion and integer precedence: what does this program print? #include<stdio.h> #define MIN(x, y) (x < y) ? x : y int main() { int x = 3, y = 4, z; z = MIN(x + y/2, y - 1); if (z > 0) printf("%d\n", z); return 0; }

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.
  • Macro: #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:

Evaluate 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:

4 or 5 would be chosen only if computations differed; they do not.0/No output contradict the positive result.


Common Pitfalls:
Forgetting integer truncation; misreading operator precedence (division before addition and subtraction).


Final Answer:
3.

More Questions from C Preprocessor

Discussion & Comments

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