25

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?

William Strong
  • 363
  • 2
  • 4
  • 3
    These are very good questions for a student. Don't be discouraged if anyone dismisses them. – Kristopher Johnson Dec 14 '17 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. – supercat Dec 14 '17 at 21:05
  • 2
    Reopened. The question is about why `_Bool` is so named; not about "what is `_Bool`" – M.M Dec 14 '17 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. – Stargateur Dec 14 '17 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 – M.M Dec 14 '17 at 21:24
  • Relevant reading for question two https://stackoverflow.com/questions/47374356/why-use-stdbool-h-instead-of-bool/47374416#47374416 – StoryTeller - Unslander Monica Dec 14 '17 at 21:24
  • @StoryTeller this thread is a dupe of that one tbh – M.M Dec 14 '17 at 21:25
  • @M.M - I'm torn. The answers are surely related. But the questions are vastly different. – StoryTeller - Unslander Monica Dec 14 '17 at 21:26
  • @M.M update a code program to a higher version of the language require some work. Tool could easily be create to remove `typedef bool`. Keep legacy problem is the first problem of computer programming. People are afraid of change, python2 was a great exemple, there is still a lot of people who still code in python2 because they don't like change. That really sad, C99 was ten years after C89, it's clearly a major version but no don't do breaking change with `bool` (despise the fact that C99 made a lot of breaking change). If people don't want update their code, they should keep compile in C89. – Stargateur Dec 14 '17 at 21:39
  • "why does bool have to be included" --> To be clear, `` does not need to be included. Code can use `_Bool` without the header file. `_Bool` is part of the basic functionality since C99. – chux - Reinstate Monica Dec 14 '17 at 21:41
  • I still don't understand why a boolean type is needed in C and I have never used it. Mybad? Has it caused more problems than it solved? – Weather Vane Dec 14 '17 at 21:47
  • @WeatherVane Having a true boolean type allows you to substitute `if ( expression )` with `bool x = expression; if ( x )` . And other such transformations. Without a true boolean type this code fails for some expressions, you have to use hacks like `!!expression`. – M.M Dec 14 '17 at 22:39
  • @M.M if see no advantage with your `bool x = expression; if ( x )` over `int x = expression; if ( x )`. I have never needed to use `!!` hack. Or any trouble at all with `if ( expression )`. – Weather Vane Dec 14 '17 at 22:52
  • @WeatherVane let's say `expression` is something that evaluates to `0x1000000000` (where int is 32-bit), your version will set `x` to `0` whereas the true bool version sets `x` to `1` – M.M Dec 14 '17 at 23:06
  • @M.M no, my version `int x = expression;` will set `x` to `0x1000000000`. – Weather Vane Dec 14 '17 at 23:36
  • @WeatherVane that value is larger than INT_MAX, so `int` cannot hold it – M.M Dec 15 '17 at 00:05
  • Why down votes? This question is legit and can be considered as a good question. – haccks Dec 15 '17 at 05:34

3 Answers3

26

_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.

M.M
  • 138,810
  • 21
  • 208
  • 365
3

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157