Post-increment in a return and pre-decrement in printf: what is printed? #include<stdio.h> int main() { int fun(int); int i = fun(10); printf("%d ", --i); return 0; } int fun(int i) { return (i++); }

Difficulty: Easy

Correct Answer: 9

Explanation:

Introduction / Context:This question checks your understanding of post-increment in a return expression and a subsequent pre-decrement before printing. The behavior is completely defined in C.

Given Data / Assumptions:

  • fun(10) returns 10 because post-increment returns the operand’s old value.
  • Variable i receives 10.
  • printf("%d", --i); pre-decrements then prints.

Concept / Approach:The operator i++ yields the original value and then increments the local copy. Since function parameters are by value, the caller receives 10. The pre-decrement operator --i decrements first, then yields the new value.

Step-by-Step Solution:Call fun(10): returns 10 (post-increment proceeds on a temporary inside fun).Assign i = 10.Compute --i: now i = 9.Print 9.

Verification / Alternative check:If you changed the body to return (++i);, then i in main would be 11, and --i would print 10.

Why Other Options Are Wrong:“10/11/8” do not match the exact operator sequence. Behavior is not undefined here.

Common Pitfalls:Confusing pre- and post-increment semantics; assuming pass-by-reference incorrectly modifies the caller’s variable in the callee.

Final Answer:9

More Questions from Functions

Discussion & Comments

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