IAE Academy

GATE 2026 CS1 Q.34: solution and answer

GATE 2026 · CS1Q.34 · 1 mark · NAT

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.

See the question in the paper

Answer and solution

ANSWER

The program prints 9. The inner int i exists only inside the if block, and the printf comes after that block closes, so it prints the parameter i, which is still 9.

Official answer: 9Source: Official answer key, GATE 2026 CS1 (IIT Guwahati)

  1. main sets i = 9, j = 10 and calls func(9, 10). Inside func the parameters are i = 9 and j = 10.

  2. The condition i < j is , which is true, so control enters the if block.

  3. Inside the if block, int i = 0; declares a new variable i. It hides the parameter i, but only until the } that closes the if block.

  4. The while loop uses this inner i. The body runs for inner i , which is times, so j grows by and becomes . The inner i ends at and the loop stops. None of this changes the parameter i.

  5. Look at where the braces fall in the printed program: the } that closes the if block comes before printf("%d", i);. So the printf is outside the if block, where the inner i no longer exists.

  6. In that position the name i refers to the parameter i. Nothing ever assigned to the parameter, so it still holds .

  7. The program prints 9.

KEY IDEA

A variable declared inside a block { ... } exists only until that block's closing }. While it exists it hides any outer variable with the same name. Once the block closes, the name refers to the outer variable again, which in this program is the parameter i.

WATCH OUT FOR

Treating the printf as part of the if block and printing the inner i, which ends at 10. The if block closes before the printf, so the inner i is out of scope at that point and the parameter i = 9 is printed.

From the GATE 2026 CS1 paper. Every question of this paper · Download the full paper · All 7 Functions & scope questions, with notes

More Functions & scope questions