Along these lines, there is an interesting difference between _Bool and int. C99 made it so you can compare a _Bool with true validly - just watch out for the gotcha:
#include <stdbool.h>
int main (void)
{
volatile bool x = 2;
volatile char y = 2;
volatile char a = (x == true);
volatile char b = (y == true);
return 0;
}
volatile bool x = 2;
c: 81 e0 ldi r24, 0x01 ; 1
e: 89 83 std Y+1, r24 ; 0x01
When storing 2 into a bool, it doesn't store 2! It stores 0x01, which is its internal representation of true.
volatile char y = 2;
10: 82 e0 ldi r24, 0x02 ; 2
12: 8a 83 std Y+2, r24 ; 0x02
Storing 2 into a char stores 0x02, business as usual.
volatile char a = (x == true);
14: 89 81 ldd r24, Y+1 ; 0x01
16: 8b 83 std Y+3, r24 ; 0x03
Comparing x == true, because it stuffed a 1 in there to begin with, it can just retrieve the value.
volatile char b = (y == true);
18: 9a 81 ldd r25, Y+2 ; 0x02
1a: 81 e0 ldi r24, 0x01 ; 1
1c: 91 30 cpi r25, 0x01 ; 1
1e: 09 f0 breq .+2 ; 0x22 <__zero_reg__+0x21>
20: 80 e0 ldi r24, 0x00 ; 0
22: 8c 83 std Y+4, r24 ; 0x04
Comparing y == true - true is not itself a _Bool, so this is just equivalent to (y == 1)!
So, two gotchas:
- Comparison of bool with true is done by changing the way the data is stored, which means never ever ever alias a bool as an int. If you forced it to store a 2 in there with something along the lines of *(int*)&x = 2; it would not compare that properly.
- Comparison of int with true doesn't just magically work because true is not a _Bool.
Note that I do not recommend doing this. I don't particularly like that they changed the comparison semantics for bool. It works just fine if you do it right, but will confuse the shit out of somebody who misunderstands it, and certainly is not compatible with pre-C99 code that defined itself a bool type.
C's implicit boolean semantics - just if(x) rather than if(x == true) - just works.