What’s the difference between _Bool and bool in C?

In the C programming language, _Bool is a keyword that has been officially part of the the language starting with the C99 standard. When you use _Bool as a data type, you don’t have to including anything special — it’s just another keyword. The standard says that _Bool is a data type large enough to store the values 0 and 1. In theory, only one bit is required, but in practice, it typically occupies a full byte. _Bool is considered a standard unsigned integer data type. When dealing with integer conversion, the rank of _Bool is less than the rank of all other standard integer data types.

Of course, your compiler has to support it before you can use it. Not all compilers support all features of the C99 standard, even today. So, check your compiler implementation’s documentation to see if it’s in the list of keywords.

The reason the _Bool keyword looks so strange is that the C standards committee realized lots of existing software may have already defined and used “bool”, so the the leading underscore and uppercase letter are part of the new keyword to avoid breaking existing C code. (The committee used the same keyword naming strategy for the _Complex and _Imaginary keywords, which were also added in C99, as well as the other keywords that have been added to the language since then. At the time of this writing, the C keywords with underscore prefixes are: _Alignas, _Alignof, _Atomic, _Bool, _Complex, _Generic, _Imaginary, _Noreturn, _Static_assert, and _Thread_local.)

Now, if your code doesn’t already define or use the identifiers bool, true, and false for anything, the C99 standard offers a header file stdbool.h, which defines bool as an alias for _Bool, and also defines symbols for the true and false values. C++ has had bool, true, and false as keywords since C++98, the first C++ language standard. In C, assuming your compiler implementation supports it, including stdbool.h and using bool, true, and false effectively make your code forward compatible to C++, you can avoid using the odd-looking _Bool keyword, and you can avoid rolling your own values for true and false.

In most of the organizations I’ve worked with that have C coding standards, the standard practice in new C code is to include stdbool.h and use bool, true, and false. This is the approach I recommend.

Leave a Reply