Discussion
Consider the following three ANSI-C programs, P1, P2, and P3.
| P1 | P2 | P3 |
|---|---|---|
| #include \(\lt\)stdio.h\(\gt\) int a=5; int main(){ int a=7; return(0); } | #include \(\lt\)stdio.h\(\gt\) int main(){ int a=5; int a=7; return(0); } | #include \(\lt\)stdio.h\(\gt\) int main(){ int a=5; float a=7; return(0); } |
Which one of the following statements is true?
Answer: A
Only P1 will compile without any error.
P1 declares a global int a=5; and a local int a=7; inside main. These are in different scopes, so the local declaration simply hides the global one inside main. That is legal C.
P2 declares int a twice in the same block scope. C does not allow two definitions of the same identifier in one block, so this is a redeclaration error.
P3 declares a as an int and then as a float in the same block: a conflicting-types error. So only P1 compiles, which rules out the other three options.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development