Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The initialization of static variables in C

People also ask

How are static variables initialized in C?

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. If we change the program to following, then it works without any error.

What is static initialization of a variable?

You initialize a static object with a constant expression, or an expression that reduces to the address of a previously declared extern or static object, possibly modified by a constant expression.

Is it necessary to initialize static variables C?

It is mandatory to initialize the static variable using the static keyword in C else it will return an error. The static variable is only initialized the first time when a function is called. In a static variable, the memory of a static variable is allocated.

What is the static variable in C?

Static variables can be defined inside or outside the function. They are local to the block. The default value of static variables is zero. The static variables are alive till the execution of the program.


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, 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;

[...]


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.


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