c ++ bool问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5189072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ bool question
提问by Don Lun
in c++ , the bool , is that true == 1, false == 0?? thanks
在 c++ 中, bool 是 true == 1, false == 0 吗??谢谢
回答by Andrew Marshall
false == 0
and true = !false
false == 0
和 true = !false
i.e. anything that is not zero and can be converted to a boolean is not false
, thus it mustbe true
.
即任何不为零且可以转换为布尔值的东西都不是false
,因此它必须是true
。
Some examples to clarify:
一些例子来澄清:
if(0) // false
if(1) // true
if(2) // true
if(0 == false) // true
if(0 == true) // false
if(1 == false) // false
if(1 == true) // true
if(2 == false) // false
if(2 == true) // false
cout << false // 0
cout << true // 1
true
evaluatesto 1
, but any int
that is not false
(i.e. 0
) evaluatesto true
but is not equalto true
since it isn't equal to 1
.
true
评估为1
,但任何int
不是false
(即0
)评估为true
但不等于,true
因为它不等于1
。
回答by It Grunt
Yes that is correct. "Boolean variables only have two possible values: true (1) and false (0)." cpp tutorial on boolean values
对,那是正确的。“布尔变量只有两个可能的值:真 (1) 和假 (0)。” 关于布尔值的 cpp 教程