Question
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?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2026 answer key
Solution
Check C scoping rules for global vs. local shadowing (P1) and duplicate identifier declarations in the same block scope (P2, P3).
- In P1, the first
int a=5;is in file (global) scope, whileint a=7;insidemain()is in block (local) scope. Local variablealegally shadows the globala, so P1 compiles without error. - In P2 and P3,
ais declared twice within the exact same block scope (main), causing a redefinition/conflicting-types compilation error.
Hence only P1 compiles without any error.
Final answer
Option (A)
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2026 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 19 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.