28

Why does C use the word _Bool to define boolean values? Whereas they use the word float for floats and not _Float?

Furthermore, why does bool have to be included, why isn't part of the basic functionality, like float?

17
  • 3
    These are very good questions for a student. Don't be discouraged if anyone dismisses them. Commented Dec 14, 2017 at 21:02
  • A big part of this question is why the type was given the name _Bool; a quality answer should describe the problems that would have occurred with a name that may be used in existing code, and how the use of reserved identifiers may help avoid that problem. Commented Dec 14, 2017 at 21:05
  • 2
    Reopened. The question is about why _Bool is so named; not about "what is _Bool" Commented Dec 14, 2017 at 21:06
  • In my opinion, I don't understand why committee want this backward compatibly for such trivial thing. Always include stdbool.h is boring. Commented Dec 14, 2017 at 21:17
  • @Stargateur you would understand it if you ever had to work with a large code base already set up to use bool as typedef for a C89 type Commented Dec 14, 2017 at 21:24

3 Answers 3

31

_Bool was not originally in C, but was added in the 1999 C Standard. If it had been called bool then a large amount of existing code would break because many projects made their own type alias bool already.

The C89 standard set aside identifiers starting with _ followed by upper-case character as reserved for implementation use. This is why new features added to C always start with such names. _Complex, _Alignof and _Static_assert are other examples.

There is also a header <stdbool.h> which aliases bool to _Bool and defines true and false ; this header can be included by new projects or by projects that didn't already define bool.

Sign up to request clarification or add additional context in comments.

Comments

5

C did not originally have a Boolean type, it was added in the 1999 version of the language (C99). At that point, C++ was already standardized (in 1998) to use the type bool, with keywords false and true. To keep the C Boolean type separate from the one in C++, as well as preventing the new name from breaking old C code, it was named _Bool.

The reason why it was named with an underscore followed by an upper-case letter, is because such an identifier was already guaranteed not to exist in compiler, library or user code, by 7.1.3:

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

"Reserved for any use" meaning reserved for future versions of the C language.

Therefore, all new language keywords that have been added to the language since C99 are named with underscore followed by first letter upper-case. Other examples from C99 are the types _Complex and _Imaginary.


For the cases where code compatibility with C++ was desired, the header <stdbool.h> was created. It contains the macro bool, which expands to _Bool. And also the macros false and true that expand to 0 and 1.

Though note that booleans are not fully integrated in the C language, as they are in C++. In C++, an expression such as a == b gives a result of type bool, with the value true or false. In C it gives a result of type int, with the value 1 or 0. This is for backwards-compatibility reasons with old C code.

Comments

2

As for ...

Furthermore, why does bool have to be included, why isn't part of the basic functionality, like float?

... I observe that although you need to include stdbool.h to get bool, that's a convenience and C++-compatibility feature, not an essential. bool is an alias for _Bool, and in C99 and later you have _Bool automatically, but even that is non-essential. In C, any integer or pointer value can be interpreted as a boolean, with 0 or NULL being interpreted as false and all other values being interpreted as true. This was how boolean values were handled in C from the beginning, and it still works in implementations conforming to the latest standard.

In fact, type _Bool itself is just a special case of this very behavior: it is an integer type whose minimal requirements are that it be able to represent the values 0 and 1. In boolean context, it works just the same as any other integer type.

2 Comments

There's some cases where _Bool works different than integers. Most notably floating point conversions: (_Bool)0.5 gives 1 while (int)0.5 gives 0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.