Does true equal to 1 and false equal to 0?

C++CBoolean

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDon LunView Question on Stackoverflow
Solution 1 - C++Andrew MarshallView Answer on Stackoverflow
Solution 2 - C++It GruntView Answer on Stackoverflow