Consider the following three ANSI-C programs P1, P2, and P3. Which one of the…
2026
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
Attempted by 651 students.
Show answer & explanation
Correct answer: A
Analysis of Programs
To determine which program compiles, we analyze the variable declarations in each:
Program P1
P1 declares a global variable `int a=5;`. Inside `main`, it declares a local variable `int a=7;`. In C, a local variable can shadow a global variable with the same name. This is valid syntax.
Program P2
P2 declares `int a=5;` and then `int a=7;` inside the same function scope. C does not allow redeclaring a variable with the same name in the same block. This causes a compilation error.
Program P3
P3 declares `int a=5;` and then `float a=7;` inside the same function scope. Similar to P2, redeclaring a variable name in the same block is invalid, regardless of the data type. This causes a compilation error.
Conclusion
Only P1 compiles without any error. P2 and P3 fail due to variable redeclaration errors.
A video solution is available for this question — log in and enroll to watch it.