c++ - Weird boolean conversion (?) -
explain pleasy why second expression returns false
cout << (4==4) << endl; //1 cout << (4==4==4) << endl; // 0
(4==4==4)
((4==4)==4)
(true == 4)
(1==4)
1 false
2 getting printed 0
.
note ==
has associativity left-to-right, doesn't matter (in this case) because if had associativity right-to-left, result have been same.
1. due integral promotion.
2. note 1 might tempted think 4
in (true==4)
treated true
(after 4
non-zero, hence true
). thinking might conclude (true==4)
(true==true)
true
. not how works. bool gets promoted int, instead of int bool.
Comments
Post a Comment