Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ bool question [duplicate]

Tags:

c++

boolean

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

like image 559
Don Lun Avatar asked Mar 04 '11 02:03

Don Lun


1 Answers

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 evaluates to 1, but any int that is not false (i.e. 0) evaluates to true but is not equal to true since it isn't equal to 1.

like image 162
Andrew Marshall Avatar answered Sep 20 '22 20:09

Andrew Marshall