Take the 2-minute tour ×

Consider the C++ code below:

bool a = 5;
bool b = 6;
int c = (int)a + (int)b;

When I compile&run this code, c has the value 2. Does the standard guarantee that, in any compiler/platform, bool values initialized with false (0) or true (not necessarily 1) will be 1 in operations and the code above will always result in c being 2?

And in C99, including stdbool.h, is that still valid?

share|improve this question
i guess your answer is here: stackoverflow.com/questions/2725044/… –  Alessandro Pezzato Oct 5 '11 at 13:04
IMO, if you assign something other than true/false to a bool variable it should be code-reviewed back to you. –  Max Oct 5 '11 at 13:05
I just have one word: WHY? –  Roee Gavirel Oct 5 '11 at 13:07
Thanks @AlessandroPezzato for finding the duplicate –  David Rodríguez - dribeas Oct 5 '11 at 13:07
@Alessandro Pezzato In that question he asks about the keywork true, not a variable with a non 0 or 1 value assigned. –  fbafelipe Oct 5 '11 at 13:08

4 Answers 4

up vote 7 down vote accepted

Section 4.7 (integer versions) of the C++ standard says:

If the source type is bool, the value false is converted to zero and the value true is converted to one.

Section 4.9 makes the same guarantee for floating point conversions.

share|improve this answer
Thanks, this answer for C++, but is that valid for C99 (with stdbool) too? –  fbafelipe Oct 5 '11 at 13:30

For compilers, the rule is often that false is 0 and anything else will be true. However, treating bool like it is an integer type is usually considered bad form. The standard however include a rule to convert to int and you assumption is correct false = 0 and true = 1 as long as the compiler adhere to the standard!

In any case, why arithmetic with bool types?

Hope this help

share|improve this answer
Arichmetic with bool types can be useful to write less code, so you can write x += a;, instead of x += (a ? 1 : 0);. –  fbafelipe Oct 5 '11 at 13:51
Why not use x |= a; or even x = x || a; all nice and respectful of the underlying types –  Martin Oct 5 '11 at 13:54
And if x is an int, counting how many (of a set of bools) are true? I know there are ways around, but since this seems simpler, I am asking the question to know if it will always work. –  fbafelipe Oct 5 '11 at 13:57
@fbafelipe: Writing less code is never my goal. I want to write code that is easy to maintain, easy to understand, and performs well. Sometimes short code meets these goals. Doing arithmetic on bools never does. –  aaaa bbbb Oct 5 '11 at 22:42

According to the standard:

  • true converts to 1
  • false converts to 0

And he cast to int is not necessary as the conversion to int is implicit.

share|improve this answer

David Schwartz already answered for C++. For the C99 standard we have

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

Since of the standard also makes it clear that _Bool is subject to integer promotions it's clear that a _Bool will always be either 0 or 1.

share|improve this answer

Your Answer


