10

In C, at least every positive value except for 0 is treated as a boolean true. But what about a negative value? I did some tests and it seems that also negative values are treated as a boolean true. Is this a defined behaviour or implementation specific?

(I came to think about this when I saw in a question, someone promoting declaring "true" and "false" in an enum as 1 and 0.)

onemasse
  • 6,514
  • 8
  • 32
  • 37

5 Answers5

24

This is defined behavior. I'll look for the C99 standard paragraph stating as such

§ 6.3.1.2
When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • +1 The standard `bool` is good. I use an `enum` as a fallback in case a particular compiler lacks C99 support. – Chris Lutz Nov 23 '10 at 09:20
2

I believe 0 is false and everything else is true.

See @casper's reply here: thread

I would take a hint from C here, where false is defined absolutely as 0, and true is defined as not false. This is an important distinction, when compared to an absolute value for true. Unless you have a type that only has two states, you have to account for all values within that value type, what is true, and what is false.

Community
  • 1
  • 1
posdef
  • 6,498
  • 11
  • 46
  • 94
2

This is the correct behavior, in C 0 is False and everything else is True

SubniC
  • 9,807
  • 4
  • 26
  • 33
1

In C, there is no boolean type; 0 and 0.0f are considered "false" in boolean contexts, everything else is "true".

Declaring "true" and "false" in an enum is wrong, because then the following code will break:

if (2 == TRUE)

(2 should evaluate as "true", but if TRUE has been defined as 1, the two values aren't considered equal).

tdammers
  • 20,353
  • 1
  • 39
  • 56
  • 1
    Anyone testing if a value is equal to `true` is wrong, as the test shouldn't be being made in the first place. An `enum` is fine in certain situations. – Chris Lutz Nov 23 '10 at 09:13
  • There is a boolean type in C99, see my answer – SiegeX Nov 23 '10 at 09:15
  • 1
    Being a nitpick here, but the enum, when used, looks like a proper boolean type but doesn't behave like one. That's why I prefer not to have it in the first place, and just use 0 and 1 instead. – tdammers Nov 23 '10 at 09:17
  • Fair enough. I wouldn't write `if(x == true)` even if it were a proper boolean type. I just like to have a functional boolean type to return from functions sometimes. – Chris Lutz Nov 23 '10 at 09:19
-1

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.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
  • Just don't do that. `bool`, `true` and `false` are defined in "stdbool.h" and do the right thing. – Jens Gustedt Nov 23 '10 at 10:07
  • @Jens - Not all systems will have C99 support, maybe someone's compiler is really old or is made by Microsoft, and it's so easy to define a fill-in `bool` type in case someone doesn't have one that if you're going to use a `bool` type in the first place you might as well have a backup. – Chris Lutz Nov 23 '10 at 10:13
  • Then you'd better do how it is foreseen in C89, namely to have `int` for Boolean values. And define your replacement in a separate file that defines them as macros `bool == int`, `false == 0` and `true == 1`. Otherwise, once you'd have a C99 compiler, this would be a real pain to port. – Jens Gustedt Nov 23 '10 at 12:59
  • @Jens - Disagree. I don't think it matters whether you define them as macros or as an `enum` so long as you define it to work the same, use it consistently (i.e. make no assumptions about the value of `true`, don't test non-`bool` values against `bool` values, etc.), and define it conditionally for anything not supporting C99, there's no way it won't work. And `bool` isn't tremendously useful anyway, so even if something breaks, it can't really break too badly. – Chris Lutz Nov 24 '10 at 09:06