Reputation: 29096
From 6.7.8.10 in the C99 standard:
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.
Is a global variable of any type (array, structure, bitfield) always defined as a static storage
?
Upvotes: 4
Views: 695
Reputation: 153407
Is it guaranteed that global variables are always initialized to 0 with c99?
Yes and no
static void *g_v_ptr; // initialized to a null pointer
C99 details a value, but not its representation. "it has pointer type, it is initialized to a null pointer" implies that the pointer has value of a null pointer. This may or may not be NULL
. This may or may not be 0
. A compiler may have many bit patterns that correspond to a null pointer. In any case, the g_v_ptr == NULL
is true as well as g_v_ptr == 0
, but the pointer may have a different bit representation than 0
. Certainly a pattern of all zero bits is usually easy to implement and certainly the most likely implementation. Yet the spec is just squishy enough to allow for some non-zero bit pattern to be used.
A similar case can be made for floating point numbers.
In any case (IAC), the initialized value will equate to 0.
Upvotes: 4
Reputation: 121387
Yes. It's guaranteed (at least since C89). "Global variables" (either with internal or external linkage) have static storage duration. Any object with static storage is guaranteed to be zero initialized according to C99, 6.7.8 Initialization.
C99 draft, 6.2.4 Storage durations of objects:
3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
6.2.2 Linkages of identifiers describes the linkage of identifiers, particularly in relation to "global" variables:
3 If the declaration of a file scope identifier for an object or a function contains the storage- class specifier static, the identifier has internal linkage.22)
4 For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,23) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
Ignoring the case which could lead to undefined behaviour, all file scope identifiers with either internal or external linkage and they all have static storage duration.
Upvotes: 2
Reputation: 16213
Yes, as far as they are stored into bss
section.
Your linker script define it and (mainly) default linker scripts do that. While you can manually store data in different section manually, global scoped variable too.
BTW is the startup code that is responsible of zero the section. If you are working with non standard platforms or your startup code is made by you, you must ensure that.
Upvotes: 0
Reputation: 310960
According to the C Standard (5.1.2 Execution environments)
1 Tw o execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment. All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution environment.
and (6.2.4 Storage durations of objects)
3 An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
Upvotes: 3