Array partial initialization in C: given only the first two elements, what prints? #include<stdio.h> int main() { int a[5] = {2, 3}; // remaining elements implicitly zero printf("%d, %d, %d ", a[2], a[3], a[4]); return 0; }

Difficulty: Easy

Correct Answer: 0, 0, 0

Explanation:


Introduction / Context:
C allows aggregate initializers that do not list every element. It is important to know how unspecified elements are initialized, especially for local (automatic) arrays with explicit initializers.



Given Data / Assumptions:

  • int a[5] = {2, 3}; provided at definition.
  • Unspecified elements are part of the same initializer context.
  • We print a[2], a[3], a[4].


Concept / Approach:
Per C rules, when an initializer lists fewer values than the number of elements, the remaining elements are initialized as if by zero. This applies to automatic storage arrays as well when an initializer is present.



Step-by-Step Solution:
a[0] = 2, a[1] = 3.a[2], a[3], a[4] are not listed → set to 0.So printing yields “0, 0, 0”.



Verification / Alternative check:
Adding a designator or listing all five values would override the zeros; removing the initializer entirely would leave automatic array elements uninitialized (indeterminate), but that is not the case here.



Why Other Options Are Wrong:
A/E: Not indeterminate because an initializer is present. B/C: Do not reflect the rule for omitted elements being zeroed.



Common Pitfalls:
Confusing automatic uninitialized arrays (indeterminate) with partially initialized arrays (rest zero). Assuming behavior is only for static storage—partial initialization zeroing applies here too.



Final Answer:
0, 0, 0

More Questions from Declarations and Initializations

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion