In C, trace pre-decrement and post-decrement results: what is the output? #include<stdio.h> int main() { int x = 4, y, z; y = --x; z = x--; printf("%d, %d, %d ", x, y, z); return 0; }

Difficulty: Easy

Correct Answer: 2, 3, 3

Explanation:


Introduction / Context:
This question tests understanding of pre-decrement (--x) versus post-decrement (x--) and how their results affect assigned variables and subsequent operations.


Given Data / Assumptions:

  • Initial x = 4.
  • y is assigned the value of --x (pre-decrement).
  • z is assigned the value of x-- (post-decrement).


Concept / Approach:
Pre-decrement decrements the variable first, then yields the decremented value. Post-decrement yields the current value, then decrements the variable after the expression is evaluated.


Step-by-Step Solution:
Start with x = 4.y = --x → x becomes 3, then y gets 3.z = x-- → z gets current x (3), then x becomes 2.Final values: x = 2, y = 3, z = 3. Printed as "2, 3, 3".


Verification / Alternative check:
Replace the decrement lines with explicit operations to verify: x = x - 1; y = x; z = x; x = x - 1; → identical results.


Why Other Options Are Wrong:
(a) and (b) keep x at 4, which contradicts the decrements. (c) misplaces the final x and z values. (e) does not align with the actual sequence of updates.


Common Pitfalls:
Forgetting that post-decrement returns the old value, not the new value, for the current expression; mixing up print order with update order.


Final Answer:
2, 3, 3

Discussion & Comments

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