Home >Backend Development >C++ >How are Static and Global Variables Initialized in C ?
Initialization of Static and Global Variables in C
In C , static and global objects with namespace scope are initialized differently than in C. Here's a comprehensive explanation:
Initialization Phases:
C initializes these variables in three phases:
Initialization of Given Variables:
In your code snippet:
Storage and Management of Initialization Values:
During compilation, initialization values are stored in the "data" segment of the executable, unless they are const. In that case, they are placed in the "text" segment.
The system loads the "data" segment into memory, initializing static variables with static initializers. Variables with no initializers or dynamic initializers are placed in the "bss" segment, which is zeroed out by the system before code execution.
Additional Note for C 11
C 11 introduces constexpr, allowing some user-defined functions to be static initializations. Thread local variables are also introduced, which further complicates initialization procedures.
The above is the detailed content of How are Static and Global Variables Initialized in C ?. For more information, please follow other related articles on the PHP Chinese website!