全局变量与局部变量的初始化

时间:2022-01-13 21:30:40

全局变量:如果没有赋初值,默认初始化为0
局部变量,如果没有赋初值,输出随机数,

#include <iostream>

int f;
double g;
int hi[5];

int main()
{
int i = 0;
int a;
double b,c;
int d(100);
int e = 101;
int jk[5];
std::cout<<"a ="<< a << " b =" << b<<" c ="<< c << " d ="<<d<<" e="<<e<<std::endl;
std::cout<<"f = "<<f << ", g = " << g << std::endl;
for(i = 0; i < 5; i++){
std::cout<<"hi["<<i<<"]= "<<hi[i]<<",";
}
std::cout<<std::endl;
for(i = 0; i < 5; i++){
std::cout<<"jk["<<i<<"]= "<<jk[i]<<",";
}
std::cout<<std::endl;
}

结果如下:

a =-1216731688 b =4.85982e-270 c =-1.9505 d =100 e=101
f = 0, g = 0
hi[0]= 0,hi[1]= 0,hi[2]= 0,hi[3]= 0,hi[4]= 0,
jk[0]= -1219234771,jk[1]= -1217694780,jk[2]= -1216491520,jk[3]= 134515147,jk[4]= -1217695744,

static