Computer Sciences > GATE 2026 SET-2 > Programming and Data Structures
Consider the following three ANSI-C programs, P1, P2 and P3
Which one of the following statements is true?
A
Only P1 will compile without any error
B
Only P2 will compile without any error
C
Only P3 will compile without any error
D
All three programs P1, P2, and P3 will compile without any error

Correct : a

P1 declares a as a global variable and then declares another a as a local variable inside main. In C, a local variable is allowed to have the same name as a global variable. The local one simply shadows the global within that scope. This is perfectly valid ANSI-C and P1 compiles cleanly.
P2 declares int a = 5 and then immediately declares int a = 7 again inside the same function scope. Redeclaring the same variable name within the same scope is not allowed in ANSI-C, so P2 fails to compile.
P3 does the same thing but with a different type — first int a = 5 then float a = 7 in the same scope. Changing the type does not help. ANSI-C still treats this as a redeclaration error in the same scope, so P3 also fails to compile.
The key rule here is that variable shadowing across scopes (global vs local) is allowed, but redeclaration within the same scope is never allowed, regardless of whether the type changes or not.
Correct answer: A — Only P1 compiles without error ✓

Similar Questions

In C runtime environment, which one of the following is stored in heap?
#1562 MCQ
Consider the following ANSI-C program. The output of this program is ____________. (answer in integer) Note: Assume that the program compiles and runs succ...
#1606 Fill in the Blanks
Consider the following ANSI-C function. The maximum possible value that can be returned from this function is ____________. (answer in integer) Note: Ig...
#1607 Fill in the Blanks

Related Topics

ANSI C variable shadowing GATE 2026 GATE CS 2026 Set-2 Q19 C redeclaration same scope error local global variable shadowing C P1 P2 P3 compile GATE 2026 only P1 compiles without error programming languages GATE 2026 variable redeclaration C programming

Unique Visitor Count

Total Unique Visitors

Loading......