可能重复:
为什么全局变量和静态变量初始化为默认值?
看代码,
#include <stdio.h>
int a;
int main(void)
{
int i;
printf("%d %d\n", a, i);
}
Run Code Online (Sandbox Code Playgroud)
产量
0 8683508
Run Code Online (Sandbox Code Playgroud)
这里'a'用'0'初始化,但'i'用'垃圾值'初始化.为什么?
全局声明的变量被称为具有程序范围
使用static关键字全局声明的变量据说具有文件范围.
例如:
int x = 0; // **program scope**
static int y = 0; // **file scope**
static float z = 0.0; // **file scope**
int main()
{
int i; /* block scope */
/* .
.
.
*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?