IAE Academy

GATE 2026 CS2 Q.19: solution and answer

GATE 2026 · CS2Q.19 · 1 mark · MCQ

Consider the following three ANSI-C programs, P1, P2, and P3.

P1

#include <stdio.h>
int a=5;
int main(){
  int a=7;
  return(0);
}

P2

#include <stdio.h>
int main(){
  int a=5;
  int a=7;
  return(0);
}

P3

#include <stdio.h>
int main(){
  int a=5;
  float a=7;
  return(0);
}

Which one of the following statements is true?

GATE 2026 CS2 question 19: A three-column table showing three ANSI-C programs side by side (P1, P2, P3), each a short program with #include…Tap to open full size
Figure from the GATE 2026 CS2 paper. It is part of the question.
Figure description

A three-column table showing three ANSI-C programs side by side (P1, P2, P3), each a short program with #include <stdio.h> and a main() (or global + main) declaring a variable named a twice at different or the same scope, as given in the stem's code blocks above.

  1. (A)

    Only P1 will compile without any error

  2. (B)

    Only P2 will compile without any error

  3. (C)

    Only P3 will compile without any error

  4. (D)

    All three programs P1, P2, and P3 will compile without any error

Answer and solution

ANSWER

GATE CS 2026 (CS2) Q.19 — Correct option: (A) Only P1 will compile without any error.

Official answer: ASource: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. In P1, int a=5; is declared at file (global) scope, while int a=7; is declared inside main()'s block scope. A block-scope declaration is allowed to have the same name as a file-scope one; the inner a simply shadows the outer a for the duration of the block. This is legal ANSI-C — P1 compiles without any error.

  2. In P2, both int a=5; and int a=7; are declared directly inside the same block — the body of main() — with no intervening nested block. Declaring the same identifier twice with the same type in the same scope is an illegal redeclaration in ANSI C (a compiler such as gcc reports redefinition of 'a'). So P2 fails to compile.

  3. In P3, int a=5; and float a=7; are again both declared directly in the same block (main's body). This is the same same-scope redeclaration violation as P2, compounded by a conflicting type for a (int vs float). So P3 also fails to compile.

  4. Since P1 is the only one of the three programs that compiles cleanly, and both P2 and P3 fail due to illegal redeclaration of a within the same scope, the true statement is option (A): Only P1 will compile without any error.

KEY IDEA

In C, an identifier may be redeclared in a nested (inner) scope to shadow an outer declaration — that is legal — but declaring the same identifier twice within the exact same scope (the same block) is always a compile-time redeclaration error, regardless of whether the types match.

WATCH OUT FOR

Treating all three programs as "declaring a twice" as if that phrase alone decides compilability, without checking whether the two declarations sit in the SAME scope (P2, P3 — illegal) or in DIFFERENT, nested scopes (P1 — legal shadowing). Some also mistakenly believe C requires the types to differ (as in P3) before a redeclaration becomes an error, missing that P2's same-type, same-scope redeclaration is equally illegal.

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

More Functions & scope questions