The initialization of static variables in C

Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.

To initialize everything in an object, whether it's static or not, to 0, I like to use the universal zero initializer

sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};

There is no partial initialization in C. An object either is fully initialized (to 0 of the right kind in the absence of a different value) or not initialized at all.
If you want partial initialization, you can't initialize to begin with.

int a[2]; // uninitialized
int b[2] = {42}; // b[0] == 42; b[1] == 0;
a[0] = -1; // reading a[1] invokes UB

Yes, file-scope static variables are initialized to zero, including all members of structures, arrays, etc.

See this question for reference (I'll vote to close this as a duplicate, too).


Edit: this question is getting much better answers, so I'm voting to close that question as a duplicate of this, instead.

For reference, here is the C FAQ link from that question's accepted answer, although of course the C99 and C11 standards linked here are canonical.


Yes, they are, as long they have static or thread storage duration.

C11 (n1570), § 6.7.9 Initialization #10

If an object that has static or thread storage duration is not initialized explicitly, then:

[...]

  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

[...]