Scope, Lifetime & Storage Classes

Answer & solution
Official answer: 9
Step through it (42 steps)
Givens and goal
Function func(i,j) is called with i=9,j=10. If i<j, an inner block declares a new int i=0 and loops while that inner i<10, adding 2 to j and incrementing the inner i. After the block, printf("%d",i) executes. Determine the printed integer.
Approach
Track each distinct variable object and which declaration each occurrence of i refers to.
Step-by-step solution
1. The function receives its own parameter i with value 9 and parameter j with value 10. The initial comparison succeeds.
2. Inside the conditional block, a new local i is declared. It shadows the parameter i only within that block. The loop increments this inner variable from 0 to 10; it does not alter the parameter i.
3. The loop also increases the function-local j, which has no effect on the printed expression. When execution leaves the block, the inner i goes out of scope.
4. The print statement is outside the block. Its identifier i therefore refers to the parameter, still equal to 9. The output is 9, with no newline implied by the shown format string.
Common error: Shadowing creates a different object; it is not assignment to the outer object.
Scope trace
Point Parameter i Inner i Parameter j
Function entry 9 absent 10
Inner declaration 9 0 10
After ten loop iterations 9 10 30
At printf 9 absent 30The table makes clear that reaching inner i=10 does not imply that printf prints 10. The condition uses the parameter before the inner declaration is reached, whereas the loop uses the inner object.