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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:37:32  来源:igfitidea点击:

c++ bool question

c++boolean

提问by Don Lun

in c++ , the bool , is that true == 1, false == 0?? thanks

在 c++ 中, bool 是 true == 1, false == 0 吗??谢谢

回答by Andrew Marshall

false == 0and true = !false

false == 0true = !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

trueevaluatesto 1, but any intthat is not false(i.e. 0) evaluatesto truebut is not equalto truesince 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 教程