是否保证全局变量总是用c99初始化为0?

Is it guaranteed that global variables are always initialized to 0 with c99?

来自 C99 标准中的 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.

任何类型(数组、结构、位域)的全局变量是否总是定义为 static storage

是的。这是有保证的(至少从 C89 开始)。 “全局变量”(具有内部或外部链接)具有 静态存储持续时间 。任何具有静态存储的对象都保证根据 C99 6.7.8 初始化为零初始化。

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 描述标识符的链接,特别是与“全局”变量相关的链接:

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.

忽略 could lead to undefined behaviour 的情况,所有具有内部或外部链接的文件范围标识符都具有静态存储持续时间。

是的,只要它们存储在 bss 部分。

您的链接描述文件定义它并且(主要)默认链接描述文件执行此操作。 虽然您可以手动将数据存储在不同的部分,但也可以是全局范围的变量。

BTW是负责零段的启动代码。如果您使用的是非标准平台或您的启动代码是您自己编写的,则必须确保这一点。

根据 C 标准(5.1.2 执行环境)

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.

和(6.2.4 对象的存储期限)

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.

Is it guaranteed that global variables are always initialized to 0 with c99?

是也不是

static void *g_v_ptr;  // initialized to a null pointer

C99 详细说明了一个 ,但没有说明它的表示形式。 "it has pointer type, it is initialized to a null pointer" 意味着指针具有 空指针 的值。这可能是也可能不是 NULL。这可能是也可能不是 0。编译器可能有许多对应于 空指针 的位模式。在任何情况下,g_v_ptr == NULLg_v_ptr == 0 都为真,但指针可能具有与 0 不同的位表示形式。当然,全零位的模式通常很容易实现,当然也是最有可能实现的。然而,该规范还不够灵活,可以使用一些 non-zero 位模式。

浮点数也有类似的情况。

在任何情况下 (IAC),初始化值将等于 为 0。