Question:

Consider the following program in C:
#include <stdio.h>

void func(int i, int j) {
  if (i < j) {
    int i = 0;
    while (i < 10) {
      j += 2;
      i++;
    }
  }
  printf("%d", i);
}

int main() {
  int i = 9, j = 10;
  func(i, j);
  return 0;
}
The output of the program is __________. (answer in integer)
Note: Assume that the program compiles and runs successfully.

Show Hint

The declaration \(int\ i = 0;\) inside the if block creates a new variable that only shadows the outer parameter \(i\) within that block. Once the block ends, that shadow variable disappears and the original parameter value reappears unchanged for the final printf.
Updated On: Jul 22, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 9

Solution and Explanation

Step 1: Trace the call from main.

In main, \(i = 9\) and \(j = 10\) are local variables. The call \(func(i, j)\) passes copies of these values, so inside func the parameter \(i\) starts at 9 and the parameter \(j\) starts at 10. C passes arguments by value, so any changes inside func to these parameters do not affect main's variables, but that is not even needed to answer this question since the output happens inside func itself.

Step 2: Evaluate the if condition.

The check is \(i < j\), that is \(9 < 10\), which is true, so the block inside the if statement executes.

Step 3: Understand the variable shadowing inside the block.

Inside the if block, the line \(int\ i = 0;\) declares a brand new variable named \(i\) that exists only within this inner block scope. In C, this inner \(i\) hides, or shadows, the outer parameter \(i\) for the rest of the block. From this point until the closing brace of the if block, any reference to \(i\) refers to this new, block-local variable, not to the function parameter.

Step 4: Run the while loop.

The loop \(while\ (i < 10)\ \{\ j\ {+}{=}\ 2;\ i{+}{+};\ \}\) operates entirely on the block-local \(i\), which starts at 0. It runs while \(i < 10\), so it executes for \(i = 0,1,2,\ldots,9\), a total of 10 iterations, after which \(i\) becomes 10 and the loop stops. During these 10 iterations, \(j\) is incremented by 2 each time, and since \(j\) is not redeclared inside the block, this modifies the actual parameter \(j\), which goes from 10 up to \(10 + 10 \times 2 = 30\).

Step 5: Exit the block and check what happens to the outer i.

When the closing brace of the if block is reached, the block-local variable \(i\) goes out of scope and ceases to exist. Control now refers back to the outer parameter \(i\), the one passed into func, which was never touched during the block, because the block only ever modified its own separate, shadowed copy. So the parameter \(i\) still holds its original value of 9.

Step 6: Evaluate the printf statement.

The statement \(printf("\%d", i);\) executes after the if block has ended, so \(i\) here refers to the outer parameter, whose value is 9. The updated value of \(j\) (which is 30) is never printed, since only \(i\) is passed to printf.

Step 7: Conclusion.

The program prints 9.
\[ \boxed{9} \]
Was this answer helpful?
0
0