Skip to content
Official PYQ · 2026

GATE CS 2026 Set 2 Q9 — Programming in C

Explain with AI

Consider the following three ANSI-C programs, P1, P2,\text{P1, P2,} and P3\text{P3}.

P1P2P3#include <stdio.h>#include <stdio.h>#include <stdio.h>int a=5;int main(){int a=7;return(0);int main(){int a=5;int a=7;return(0);int main(){int a=5;float a=7;return(0);}}}\begin{array}{|l|l|l|} \hline \quad\quad\quad \textbf{P1} & \quad\quad\quad \textbf{P2} & \quad\quad\quad \textbf{P3} \\ \hline \text{\#include <stdio.h>} & \text{\#include <stdio.h>} & \text{\#include <stdio.h>} \\ \begin{array}{l} \text{int a=5;} \\ \text{int main()\{} \\ \quad \text{int a=7;} \\ \quad \text{return(0);} \end{array} & \begin{array}{l} \text{int main()\{} \\ \quad \text{int a=5;} \\ \quad \text{int a=7;} \\ \quad \text{return(0);} \end{array} & \begin{array}{l} \text{int main()\{} \\ \quad \text{int a=5;} \\ \quad \text{float a=7;} \\ \quad \text{return(0);} \end{array} \\ {\text{\}}} & {\text{\}}} & {\text{\}}} \\ \hline \end{array}

Which one of the following statements is true?

 

Choose one option
View worked solution

Answer: A

To determine which of the programs compile successfully, let us analyze each program's scope and variable declarations:

  • Program P1:
    Here, int a = 5; is declared as a global variable, and int a = 7; is declared as a local variable inside main(). In C, a local variable can have the same name as a global variable; the local variable simply shadows the global variable within its scope. This is perfectly valid, so P1 compiles without any error.
  • Program P2:
    Inside main(), the variable a is declared twice (int a = 5; and then int a = 7;) within the same block scope. Redefining a variable with the same name in the same scope is a compilation error (redefinition/redeclaration error). Thus, P2 fails to compile.
  • Program P3:
    Similar to P2, the variable a is declared twice in the same scope, first as int and then as float. This is a conflicting type redeclaration error. Thus, P3 fails to compile.

Therefore, only P1 compiles without any error.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions