Which one of the following statements is true?

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
Total Unique Visitors