C语言 全局变量在 C 中总是初始化为零吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16015656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are global variables always initialized to zero in C?
提问by Alex
#include <stdio.h>
int a[100];
int main(){
printf("%d",a[5]);
return 0;
}
Does the above code always print '0' or is it compiler specific? I'm using gcc compiler and I got the output as '0'.
上面的代码总是打印“0”还是编译器特定的?我正在使用 gcc 编译器,输出为“0”。
回答by simonc
Yes, all members of aare guaranteed to be initialised to 0.
是的,a保证所有成员都被初始化为 0。
From section 3.5.7 of the C89 standard
来自 C89 标准的第 3.5.7 节
If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.
如果没有显式初始化具有静态存储持续时间的对象,则它会被隐式初始化,就好像每个具有算术类型的成员都被分配了 0,并且每个具有指针类型的成员都被分配了一个空指针常量。
回答by Lundin
"Global variables" are defined at file scope, outside any function. All variables that are defined at file scope and all variables that are declared with the keyword statichave something called static storage duration. This means that they will be allocated in a separate part of the memory and exist throughout the whole lifetime of the program.
“全局变量”在文件范围内定义,在任何函数之外。在文件范围内定义的所有变量和使用关键字声明的所有变量static都有称为静态存储持续时间的东西。这意味着它们将被分配在内存的一个单独部分,并存在于程序的整个生命周期中。
It also means that they are guaranteed to be initialized to zero on any C compiler.
这也意味着它们在任何 C 编译器上都保证被初始化为零。
From the current C standard C11 6.7.9/10:
从当前的 C 标准 C11 6.7.9/10 开始:
"... If an object that has static or thread 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;"
"...如果没有显式初始化具有静态或线程存储持续时间的对象,则:
——如果是指针类型,则初始化为空指针;
— 如果它具有算术类型,则将其初始化为(正或无符号)零;”
Practically, this means that if you initialize your global variable to a given value, it will have that value and it will be allocated in a memory segment usually referred to as .data. If you don't give it a value, it will be allocated in another segment called .bss. Globals will never be allocated on the stack.
实际上,这意味着如果您将全局变量初始化为给定值,它将具有该值,并将分配在通常称为 的内存段中.data。如果你不给它一个值,它将被分配到另一个名为.bss. 永远不会在堆栈上分配全局变量。
回答by Sam
Yes.
Any global variable is initialized to the default valueof that type.
0is the default value and is automatically casted to any type.
If it is a pointer, 0becomes NULL
是的。任何全局变量都被初始化为该类型的默认值。
0是默认值并自动转换为任何类型。如果是指针,则0变为NULL
Global variables get there space in the data segmentwhich is zeroed out.
全局变量在被清零的数据段中获得空间。
It is notcompiler specific but defined in the C standard.
它不是特定于编译器的,而是在 C 标准中定义的。
So it will always print 0.
所以它总是打印0。
回答by ouah
File scope objects declared without explicit initializers are initialized by 0by default (and to NULLfor pointers).
没有显式初始化器声明的文件范围对象0默认初始化(和 toNULL指针)。
Non-static objects at block scope declared without explicit initializers are left uninitialized.
没有显式初始化程序声明的块范围内的非静态对象未初始化。
回答by MOHAMED
Are the globle variable always initalized to zero in C?
全局变量在 C 中是否总是初始化为零?
Yes and It's defined in the C standard.
是的,它在 C 标准中定义。
回答by Prasoon Saurav
It is not compiler specific. The code will always print 0.
它不是特定于编译器的。该代码将始终打印0。

