Skip to the content of the web site.

Lesson 311: Built-in Boolean type

Previous lesson Next lesson


Up to now, we have used a Boolean type bool. When the C language was first designed, however, it would have been absurd to devote an entire byte to storing either true or false. Instead, recall that the C programming language was designed to write operating systems, and usually there are more than one Boolean-valued flags required in any situation, and therefore one byte could store eight Boolean-valued flags.

Today, however, in the interest of best software engineering practices, it is sometimes useful to use just a single variable for a single Boolean flag, as this reduces the likelihood of interference through erroneous code. The C++ programming language introduced the built-in bool type and with the 1999 C Standard (C99), it was determined that such a type should be added to C, as well.

The problem at the time was that a lot of code between 1970 and 1999 was likely to use bool either as a defined type or as a variable name, and introducing a new keyword bool would immediately invalidate all of these programs.

Thus, the actual build-in type is _Bool, and as all identifiers starting with an underscore are reserved for the compiler, no user code should be using this identifier. If backwards comparability does not allow you to use the bool type, you can still use _Bool.

The stdbool.h header file has three defined identifiers:

#define bool  _Bool
#define true  1
#define false 0

Thus, once you load the stdbool.h header file, you can use the bool type just like you do in C++.

If, however, you are learning the C programming language to write real-time embedded systems, you may consider learning about bit flags.


Previous lesson Next lesson