bool型变量各种初始化情况的值
1. bool 型数组为局部变量且未初始化的情况
#include<iostream>
using namespace std;
int main()
{
bool book[100];
for(int i=0;i<100;i++)
cout<<book[i]<<" ";
return 0;
}
输出如下:
结果:随机赋值。
2. bool型数组定义为局部变量且初始化赋值为false
#include<iostream>
using namespace std;
int main()
{
bool book[100]={false};
for(int i=0;i<100;i++)
cout<<book[i]<<" ";
return 0;
}
输出如下:
结果:值全为false。
3. bool型数组定义为局部变量且初始化赋值为true
#include<iostream>
using namespace std;
int main()
{
bool book[100]={true};
for(int i=0;i<100;i++)
cout<<book[i]<<" ";
return 0;
}
输出如下:
结果:除开book[0]=true,其他值全为false。
4. bool型数组定义为全局变量
#include<iostream>
using namespace std;
bool book[100];
int main()
{
for(int i=0;i<100;i++)
cout<<book[i]<<" ";
return 0;
}
输出如下:
结果:值全为false。