In ISO standard C, my understanding is that there is nothing that actually nails down the the representation of a _Bool, but it does say:
- "_Bool is large enough to hold the values 0 and 1"
- "When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1"
- "number of bits in a _Bool is atleast CHAR_BIT the width of a _Bool can be just 1 bit"
I am thinking then (and from other related answers), that the representation of false need not actually be 0 (even though in nearly all implementations, it is). So what happens if you memset a _Bool to 0, then use it somehow? Is this undefined behavior (by default because it is not defined in the standard) or implementation defined behavior? This seems to matter (in my understanding) because in the former case, it's not a well defined C program, in the latter it is. For example is this undefined behavior? Can false have a representation other than 0?
#include <stdbool.h>
//...
bool x = true;
memset(&x, 0, sizeof(bool));
if(x == true)
{
printf("Zero is true!");
}
else
{
printf("zero is false!");
}