Need for _Bool in C99?

c, c++

Solution

It says that C99 added a variable _Bool

No, C99 added a built-in type called `_Bool`, which can have values of either `0` or `1`. The header, `<stdbool.h>` defines macros in which `bool` expands to `_Bool`, `false` to `0`, and `true` to `1`.

C++, on the other hand, has a built in type called `bool`, which can have values of `true` and `false`. For compatibility, C++11 specifies that `stdbool.h` should be present, but empty. (Some C compilers provided C++'s `bool` as an extension pre-C99.)

The intention with the C99 additions was to provide the same facilities as C++, but in a way that didn't invalidate old C89 code (where plain `bool` was available as a name). In my opinion the macro solution they came up with is less than ideal, and indeed it's still quite rare to see C code which uses the boolean types, whereas they are pervasive in C++.

Problem

I am reading a book on C. It says that C99 added a data type _Bool. It is basically an int but stores only 0 or 1. Now I do not understand why there is a need of such a data type. We already have bool which implicitly translates to int and vice versa. So can somebody please tell me a situation where such a data type would be useful. PS: C++ does not seem to support such a data type as seen here. ``` #include <iostream> using namespace std; int main() { // your code goes here _Bool b = false; if(b == 0) printf("FALSE"); else printf("TRUE"); return 0; } ```

Original source

Related problems