The GATE Grind

GATE 2026 CS (CS1) – Question 34

Programming and Data Structures · Programming in C · 1 mark · Numerical answer

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)

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 9

Explanation

Trace the execution of `main()`:
1. Variables `i = 9` and `j = 10` are passed by value to `func(9, 10)`.
2. In `func(int i, int j)`, the formal parameters are initialized to `i = 9`, `j = 10`.
3. The condition `if (i < j)` evaluates to `9 < 10`, which is true.
4. Inside the `if` block, a new block-scoped local variable `int i = 0;` is declared. This shadows the parameter `i` within the block.
5. The while loop runs 10 times, incrementing `j` and the inner `i` until the inner `i` reaches 10.
6. Exiting the `if` block destroys the inner `i`.
7. `printf("%d", i);` accesses the function-scoped parameter `i`, which was never modified and retains its initial value of 9.

Thus, the program prints 9.