C defines 0 as false and everything else as true. Positive, negative, whatever.
I believe I recently advocated the use of typedef enum { false, true } bool;
so I'll own up to it. (If my original code didn't have a typedef
involved, that was an error of judgement on my part.) All nonzero values are true, so I wouldn't advocate using an enumerated bool
type for things like this:
if(x == true) // not what you want
if(!x == false) // works, but why so much effort?
I generally perfer simply if(x)
or if(!x)
to explicit tests against boolean values. However, sometimes it's good to have a boolean type:
bool is_something(int x)
{ // assume for the sake of an argument that the test is reasonably complex
if(/* something */) return false;
if(/* something else */) return true;
return false;
}
This is no better than having the type be int
, but at least you're being explicit with what the result is meant for.
Also, as per someone else above, a better bool
might be:
typedef enum { false, true = !false } bool;
I believe !
is guaranteed to return 0 or 1, but I could be wrong, and the above works well either way.