printf help

int main()
char boolean[][6]={"TRUE","FALSE"};
printf("%s",boolean[(unsigned int)-1 == ~0]);

i dont understand the (unsigned int)-1== ~0

can any one tell me how it works
Here (unsigned int)-1 means converting -1 to an equivalent unsigned integer value i.e the max value of unsigned int.

The bitwise complement operator, the tilde, ~, flips every bit.
~0 means all 0 bits are flips to 1 then here also we get same value as above i.e max value of unsigned int.

Finally (unsigned int)-1== ~0 becomes true and it displays
Last edited on
The real puzzle is the character string.
    char boolean[][6]={"TRUE","FALSE"};


This outputs "TRUE" for a value of false and vice versa.
That's because the bool type can be converted to an integer so that false=0 and true=1. That means the array index for false is zero, meaning the first string, i.e. "TRUE".

To my mind, it would make more sense to initialise the array like this: char boolean[][6]={"FALSE", "TRUE"};

However, this is mixing C++ and C code and maybe there is some reason for original code.
Topic archived. No new replies allowed.