What is the output of this C code?
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
x = 4;
}
printf("%d", x);
}
Answer: D
Correct answer: 3 4
The inner x = 4; is an assignment, not a declaration, so it changes the x already in scope - main's local. The first printf shows 3 and the second, after the block, shows 4. The file-scope x = 5 is shadowed throughout and never seen.
Please log in or register to report this question.