In C, static storage and default initialization: what is printed?
#include
int main()
{
static int a[20];
int i = 0;
a[i] = i;
printf("%d, %d, %d
", a[0], a[1], i);
return 0;
}
-
A1, 0, 1
-
B1, 1, 1
-
C0, 0, 0
-
D0, 1, 0
-
E0, 0, 1
Answer
Correct Answer: 0, 0, 0
Explanation
Introduction / Context:C guarantees that objects with static storage duration (including static arrays) are zero-initialized if not explicitly initialized. This question tests that rule and simple indexing.
Given Data / Assumptions:
- The array a is declared as static at function scope, so it has static storage duration.
- No explicit initializer is provided, implying default zero-initialization.
- i is set to 0, and a[i] is assigned 0.
Concept / Approach:Because a is static, all 20 elements start at 0. Setting a[0] = i (which is 0) does not change anything. a[1] remains 0 because no code modifies it. The variable i remains 0 as well.
Step-by-Step Solution:Initial state: a[0..19] = 0 due to static zero-initialization; i = 0.Execute a[i] = i → a[0] = 0.No statements touch a[1] → it stays 0.printf prints a[0], a[1], i → 0, 0, 0.
Verification / Alternative check:Changing static to automatic (non-static) and leaving it uninitialized would produce indeterminate values for local non-static arrays, but that is not the case here. The standard mandates zero-initialization for static storage duration.
Why Other Options Are Wrong:(a), (b), (d), (e) imply nonzero values that contradict default zero-initialization and the assignments shown.
Common Pitfalls:Confusing static storage duration with the static keyword used for internal linkage at file scope; here it controls storage duration and initialization semantics.
Final Answer:0, 0, 0