Multiple choice

int a [8] = { 0, 1, 2, 3 };

The definition of a above explicitly initializes its first four elements. Which one of the following describes how the compiler treats the remaining four elements?

  1. Standard C defines this particular behavior as implementation-dependent. The compiler writer has the freedom to decide how the remaining elements will be handled.

  2. The remaining elements are initialized to zero(0).

  3. It is illegal to initialize only a portion of the array. Either the entire array must be initialized, or no part of it may be initialized.

  4. As with an enum, the compiler assigns values to the remaining elements by counting up from the last explicitly initialized element. The final four elements will acquire the values 4, 5, 6, and 7, respectively.

  5. They are left in an uninitialized state; their values cannot be relied upon.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In C, when an array is partially initialized, all remaining elements are automatically initialized to zero. The compiler fills the uninitialized elements (a[4] through a[7]) with zeros. This is guaranteed by the C standard and not implementation-dependent.