46

I have a question about the initialization of static variables in C. I know if we declare a global static variable that by default the value is 0. For example:

static int a; //although we do not initialize it, the value of a is 0

but what about the following data structure:

typedef struct
{
    int a;
    int b;
    int c;
} Hello;

static Hello hello[3];

are all of the members in each struct of hello[0], hello[1], hello[2] initialized as 0?

0

5 Answers 5

66

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
4
  • 1
    Can you please explain a bit more this line? a[0] = -1;
    – lfalkau
    Feb 20, 2021 at 9:11
  • 2
    It is simple assignment. After a was defined (and left uninitialized) reading either a[0] or a[1] invokes UB. After the assignment to a[0] you can safely read from there, but not from a[1].
    – pmg
    Feb 20, 2021 at 9:31
  • What does "UB" mean?
    – Michael
    Oct 25 at 15:23
  • UB is Undefined Behaviour. See, eg, stackoverflow.com/questions/2397984/…
    – pmg
    Oct 25 at 16:44
3

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;

[...]

2
  • 1
    The draft of the C11 Standard (PDF document) is freely available online.
    – pmg
    Nov 6, 2012 at 12:48
  • "All objects with static storage duration shall be initialized (set to their initial values) before program startup." (5.1.2.2.2 in the c99 pdf) Which addresses my semi-related question. If you use a static variable because you want it to retain its value (like for a running total) across function calls should you manually initialize it to 0? No.
    – Alan Corey
    Nov 13, 2018 at 17:11
2

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.

1

I would add that static variables (or arrays) are classified into two types.

Initialized are the ones that are given value from code at compile time. These are usually stored in DS though this is compiler specific.

The other type is uninitialized statics which are initialized at run time and are stored into BSS segment though again this is compiler specific.

BSS

0

For the ones who don't want to read the standard, it's also mentioned in https://en.cppreference.com/w/c/language/initialization :

Implicit initialization

If an initializer is not provided:

  • objects with automatic storage duration are initialized to indeterminate values (which may be trap representations)
  • objects with static and thread-local storage duration are zero-initialized

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.