GATE 2026 CS (CS2) – Question 19
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?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (A) Only $P1$ will compile without any error.
Explanation
$P1$ is valid because the local variable `a` inside `main()` simply shadows the global `a`. In $P2$, the variable `a` is redeclared in the same scope, which is an error. In $P3$, the second declaration of `a` in the same scope with a different type also causes an error. Therefore, only $P1$ compiles without error, so option (A) is correct.