Initialization global and static variable to 0 is always unnecessary?

后端 未结 2 659
广开言路
广开言路 2020-12-10 01:00

C standard guarantees that global and static variables, if not initialized, are always 0.

Here\'s my question: the un-initialized global and static vari

2条回答
  •  萌比男神i
    2020-12-10 02:01

    The standard guarantees that a pointer with static storage duration and no other initializer will be initialized to be a null pointer, regardless of what bit pattern that may involve.

    The same basic idea applies to floating point and integer types as well -- they're guaranteed to be initialized to 0 or 0.0 as well. The implementation can leave this to the fact that BSS sets all bits to 0 if and only if it "knows" that doing so will result in the correct values.

提交回复
热议问题