开发者

Does true equal to 1 and false equal to 0? [duplicate]

开发者 https://www.devze.com 2023-02-14 13:50 出处:网络
This question already has answers here: Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?
This question already has answers here: Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int? 开发者_开发知识库 (6 answers) Closed 7 years ago.

I was wondering, does true equal to 1 and false equal to 0 and how?


false == 0 and true = !false. I.e. anything that is not zero and can be converted to a boolean is not false, thus it must be 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 is equal to 1, but any non-zero int evaluates to true but is not equal to true except 1.


Yes that is correct. "Boolean variables only have two possible values: true (1) and false (0)." cpp tutorial on boolean values

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号