Search code examples
cstandards

Are global variables always initialized with zeroes?


Can I trust of this one:

Section 6.7.8p10 Initialization of C standard

And will it always be set to 0? Are there exceptions to this rule (maybe compiler-specific ones)?


Solution

  • Yes, if the compiler is a C compiler, all static variables that are not initialized otherwise will be initialized as if by { 0 }. This means that floats and pointers will be as if initialized by { 0 } even if the bit-pattern were different.

    If the compiler has an option for it to become a not-C compiler then all bets are off.