Difficulty: Easy
Correct Answer: 20 40
Explanation:
Introduction / Context:
This question tests your understanding of C block scope, variable shadowing, and how printf prints values from the innermost visible declaration. Many beginners confuse the two X variables and expect the same value printed twice. Recognizing scope boundaries is essential for writing maintainable, bug-free C code.
Given Data / Assumptions:
Concept / Approach:
In C, the innermost declaration of an identifier shadows any outer declarations of the same name within that inner block. Therefore, references to X inside the inner braces use the inner X. When the program exits the inner block, the inner X goes out of scope and any further references use the outer X.
Step-by-Step Solution:
Enter main: outer X = 40.Enter inner block: inner X = 20 shadows outer X.printf("%d ", X) inside inner block prints 20 followed by a space.Exit inner block: inner X is destroyed; outer X (40) becomes visible again.printf("%d\n", X) after the block prints 40 and a newline.
Verification / Alternative check:
Mental trace or debugger watch window will show two distinct storage locations for X. The first printf refers to the inner one (20), and the second refers to the outer one (40).
Why Other Options Are Wrong:
40 40: would imply no shadowing.20: misses the second print after the block.Compilation error: standard, valid C; no error.40 20: reverses the actual order of prints.
Common Pitfalls:
Forgetting that an inner declaration with the same name hides the outer variable; assuming updates to the inner X affect the outer X.
Final Answer:
20 40
Discussion & Comments