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

Watch variable scope: the i declared inside the if-block is a separate variable from the outer parameter i, and it goes out of scope before the final printf runs.
Updated On: Jul 7, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 9

Solution and Explanation

Step 1: In main, \(i = 9\) and \(j = 10\), and the call is func(i, j), so inside func the formal parameters receive \(i = 9\), \(j = 10\).

Step 2: The condition \(if (i < j)\) compares parameter i (9) with parameter j (10). Since \(9 < 10\) is true, the if-block executes.

Step 3: Inside this block, a new local variable \(i = 0\) is declared with int. This inner i is a completely separate variable that shadows the outer parameter i, but only inside this inner block.

Step 4: The loop \(while (i < 10)\) refers only to this inner i. It runs 10 times, incrementing inner i from 0 up to 10 (and adding 2 to j each time), and never touches the outer parameter i.

Step 5: When the inner block ends, the inner i goes out of scope and is discarded. The printf statement, which lies outside the if-block, then uses the outer parameter i, whose value was never changed by the loop - it is still 9.

Final Answer: The program prints 9. \[\boxed{9}\]

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions

Top GATE CS Programming in C Questions