Discussion
Predict the output of the following C source code :
int x = 10;
int main( )
{
int x = 20;
{
int x = 30;
{extern x; printf("%d", x);}
}
return 0;
}
Answer: A
The program prints 10.
The innermost block declares extern x;, which (with the old implicit int) declares that x refers to an object with external linkage. That is the global int x = 10, not either local x, so within that block printf prints 10.
Without the extern declaration, the innermost local x = 30 would be printed. The value 20 belongs to the outer local that is hidden here, and only one printf runs, so '10 20 30' is impossible.
No comment is present. Be the first to comment.
Loading…
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