Question:medium

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: Aug 3, 2026
Show Solution

Correct Answer: 9

Solution and Explanation

The key idea in this question is variable scope, not arithmetic. main() calls func(9, 10), so inside func the parameter i starts as 9 and parameter j starts as 10.

The check \(i < j\) becomes \(9 < 10\), which is true, so execution enters the inner block of the if statement.

Right at the top of that inner block, the line int i = 0 creates a fresh, independent variable also named i. C allows this because each block has its own scope: this new i exists only from its declaration to the closing brace of the if-block, and while it exists it hides the outer i.

The while loop condition \(i < 10\) and the update \(i++\) inside the loop both operate purely on this fresh inner i, taking it from 0 up to 10; j is also updated 10 times but that has no effect on the printed output. None of this touches the original parameter i sitting outside the block.

Once execution passes the closing brace of the if-block, the inner i disappears since its scope has ended, and the name i reverts to meaning the outer parameter, still holding its original value 9. The printf call after the if-block therefore prints \(i = 9\).

Was this answer helpful?
0


Questions Asked in GATE CS exam