int数组和char数组都可用这种方式初始化为0
#include<iostream>
using namespace std;
int a[10];
int main(){
int i;
for(i=0;i<10;i++){
cout<<a[i]<<endl;
}
}
输出为
0
0
0
0
0
0
0
0
0
0
--------------------------------
Process exited with return value 0
Press any key to continue . . .
#include<iostream>
using namespace std;
int main(){
int i;
int a[10];
for(i=0;i<10;i++){
cout<<a[i]<<endl;
}
}
输出为
2686672
2686728
2686916
1966836949
658210400
-2
1966739810
1967086532
4250176
2686868
--------------------------------
Process exited with return value 0
Press any key to continue . . .
char数组
#include<bits/stdc++.h>//c++的万用头文件
using namespace std;
char A[6],B[6];
int main(){
int i;
for(i=0;i<6;i++){
cout<<"A["<<i<<"]="<<(int)A[i]<<' '<<"B["<<i<<"]="<<(int)B[i]<<endl;
}
cin>>A;
cin>>B;
cout<<A<<endl<<B;
}
之前在学习c的时候,老师介绍的初始化方法是
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10]={1};
for(int i=0;i<10;i++){
cout<<a[i]<<endl;
}
}
这样a[0]=1,其他的都是0。