Are the members of a c struct guaranteed to be initialized to 0?
c
Solution
Relevant parts of the C99 standard:
section 6.2.4, §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.
section 6.2.4, §4:
An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.
section 6.2.4, §5 (regarding objects with automatic storage duration):
The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.
section 6.7.8, §10:
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.
Problem
When I declare a struct in C, am I guaranteed that the members will be initialized to some specific value, such as 0 for integer members? EDIT: So, let's say I have a struct that looks like: ``` typedef struct { int a; } my_str; ``` and I declare: ``` my_str thing1; ``` globally. As per some of the answers, thing1.a will be initialized to 0 - am I understanding that correctly?