After a little experimentation
A) !x reduces to either 0 or 1. That is for any x != 0 then !x -> 0. Similarly if x == 0 then !x -> 1. !!x could be rewritten as !(!x), so always 1 if x != 0. Maybe for some architectures !!x is faster or fewer operands than x != 0?
B) Interestingly, in both C and C++ you can't increment a bool value past
true
. I was under the impression that in C, bool was a typedef alias for char, but clearly there's more to it than just that. e.g.
[k5054@localhost]$ cat bool.c
int main()
{
bool c = false;
for(int i = 0; i < 10; ++i) {
printf("c = %d\n", c);
c += 1;
}
}
[k5054@localhost]$ gcc bool.c -o bool
[k5054@localhost]$ ./bool
c = 0
c = 1
c = 1
c = 1
c = 1
c = 1
c = 1
c = 1
c = 1
c = 1
But otherwise,
sizeof(bool) == sizeof(char) == 1
. I can't think of anything else that might distinguish between a bool or any other type. Recall that C didn't originally have any bool (or _Bool) type, and even today in both C and C++ anything that can be compared to zero has a "truth value" i.e. if x == 0 then false else true, for any type where
x == 0
has a valid interpretation.