全局变量初始化,该如何解决
全局变量初始化
全局变量是不是不用初始化?自动都是清零的?
例如:定义全局变量:
int A[100000][30];
A是不是全为0,无须再:
int i=0;
int j=0;
for(i=0;i<100000;i++){
for(j=0;j<30;j++){
A[i][j]=0;
}
}
------解决方案--------------------
测试程序
全局变量是不是不用初始化?自动都是清零的?
例如:定义全局变量:
int A[100000][30];
A是不是全为0,无须再:
int i=0;
int j=0;
for(i=0;i<100000;i++){
for(j=0;j<30;j++){
A[i][j]=0;
}
}
------解决方案--------------------
测试程序
- C/C++ code
#include<stdio.h> int A[3][3]; main() { int i=0; int j=0; for(i=0;i<3;i++){ for(j=0;j<3;j++){ printf("%d",A[i][j]); } } }
------解决方案--------------------
全局变量会自动被初始化
int 型初始化为0
char型初始化为a
------解决方案--------------------
全局变量默认都是0,无论是什么类型的。。。
------解决方案--------------------
全局默认初始化0
------解决方案--------------------
------解决方案--------------------
C++ 0x:
3.7.1 Static storage duration [basic.stc.static]
1 All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration.
3.6.2 Initialization of non-local variables [basic.start.init]
2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.
所以,全局变量int A[100000][30];会用0来初始化。
当然,也可以 int A[100000][30] = {{0}};用不着循环
------解决方案--------------------
是的。
------解决方案--------------------
全局变量+静态变量:自动初始化为0;