I was wondering, does true
equal to 1 and false
equal to 0 and how?
Asked
Active
Viewed 2.5e+01k times
2 Answers
131
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
.

q-l-p
- 4,304
- 3
- 16
- 36

Andrew Marshall
- 95,083
- 20
- 220
- 214
-
25`true == 1`. Other non-zero values are true, but are not `true`. – dan04 Mar 04 '11 at 02:55
-
@dan No, that doesn't even make sense. You basically just said `true != true`. What I think you mean to say is `true == 2` but `(int)true != 2`. `true` is not an `int` just because it can be casted to one. – Andrew Marshall Mar 04 '11 at 02:56
-
23No, he's right. I'll make the implicit conversions clear. `int(true)==1`, and `bool(2)==true`, but `2 != int(true)`. – MSalters Mar 04 '11 at 08:54
-
2I see what you're saying, updated answer to make it clear the difference between _evaluating_ to `true` and being _equal_ to `true`. – Andrew Marshall Mar 04 '11 at 09:16
-
22 is true as well?That means any non-zero is true? – Aug 04 '16 at 00:41
-
2@user6288471 Yeah, that's true. – A N Aug 17 '17 at 16:57
0
Yes that is correct. "Boolean variables only have two possible values: true (1) and false (0)." cpp tutorial on boolean values

It Grunt
- 3,300
- 3
- 21
- 35
-
bool does NOT have an integral value. C++ mandates that when converting bool to integral types `true` evaluates to `1` and false evaluates to `0`, and from integral/float types it says that a zero-Value, soo `0` and `-0` evaluate to `false`, all other values evaluàte to `true`. `bool`is an integral type but not an integer. Internally a compiler might decide to use the value 3 for `false` and `64` for true as the standard does not place any restrictions on its internal representation. – ABaumstumpf Feb 10 '22 at 11:29