在c++里面,bool类型的取值只有true和false两种,true是1,false是0,非零值自动转化成true。
类型 | 表示含义 | 字节 | 对应数值 |
---|---|---|---|
true | 真 | 1 | 1 |
false | 假 | 1 | 0 |
接下来是代码示例:
#include<iostream>
using namespace std;
int main()
{
bool a=true;
bool b=false;
if(a){
cout<<"a=ture"<<endl;
}
else
cout<<"a=false"<<endl;
if(b){
cout<<"b=ture"<<endl;
}
else
cout<<"b=false"<<endl;
return 0;
}
运行图:
如果直接输出bool,是不会输出true和false,而是输出对应数值。
代码示例:
#include<iostream>
using namespace std;
int main()
{
bool a=true;
bool b=false;
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
运行图: