Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C booleans called _Bool?

Tags:

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?

like image 398
William Strong Avatar asked Dec 14 '17 20:12

William Strong


People also ask

Why does C not have booleans?

C is a very close-to-the-metal language, and abstracting true/false into a new data type that is not reflective of something that hardware can uniquely understand didn't make much sense (a bool would simply be a char with additional limitations, simple enough to typedef yourself if you really need it).

Where is _bool defined?

The _Bool type is a new type appearing in the standard C99. It is an unsigned integer type. Its range of values it has to be able to hold the values 0 and 1. The range of values of _Bool is contained in the range of values of any other unsigned integer type.

When did bool add C?

An introduction to how to use booleans in C C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”. Anyway, we do have a bool type.

Is there bool in C?

In C there is no predefined datatype as bool. We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1.


2 Answers

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

like image 93
M.M Avatar answered Oct 21 '22 14:10

M.M


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.

like image 22
Lundin Avatar answered Oct 21 '22 15:10

Lundin