Coding

Bool in C: stdbool.h, true and false Explained

C has a boolean type: _Bool since C99, spelled bool when you add #include <stdbool.h>. In C23, bool, true and false are keywords, so no header is needed.

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool is_ready = true;
    bool has_error = false;

    if (is_ready && !has_error) {
        printf("go\n");
    }
    return 0;
}

Example output: go

What stdbool.h defines

The header defines four macros. According to cppreference's page on the Boolean type support library, they expand like this:

NameExpands to
bool_Bool
trueinteger constant 1
falseinteger constant 0
__bool_true_false_are_definedinteger constant 1

The type itself, _Bool, was introduced in C99. The header gives that type the friendlier name bool, along with true and false, only to code that asks for it.

Is bool a keyword in C?

It depends on the language standard you compile with:

  • C89 / C90: there is no boolean type. Programs use int with 0 and 1, or an enum.
  • C99, C11, C17: the keyword is _Bool. bool, true and false exist only after you include <stdbool.h>.
  • C23: cppreference's keyword list marks bool, true and false as keywords introduced in C23, and _Bool as the older C99 keyword. The header is no longer required to use the names.

If a project has to build with more than one standard, keep the #include <stdbool.h> line. It costs nothing and keeps older compilers happy. Which standard your compiler uses is set by a flag such as -std=c99 or -std=c23; check your compiler's documentation for the exact spellings it accepts.

How values convert to bool

A bool never holds anything except 0 or 1. When you assign another value, C converts it first. Per cppreference's page on implicit conversions, a value of any scalar type converts to _Bool like this: a value that compares equal to zero (a zero number, or a null pointer) becomes 0, and every other value becomes 1.

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool from_int = 42;
    bool from_zero = 0;
    bool from_fraction = 0.5;
    int *null_pointer = NULL;
    bool from_pointer = null_pointer;

    printf("%d %d %d %d\n", from_int, from_zero, from_fraction, from_pointer);
    return 0;
}

Example output: 1 0 1 0

Notice the fraction. Converting 0.5 to an int would give 0, but converting it to a bool gives 1, because 0.5 is not equal to zero. The same page notes that 0.0 / 0.0 (NaN) converts to 1 as well, since NaN does not compare equal to zero.

Common bool mistakes in C

Comparing an int to true

Because true is the constant 1, comparing a plain int to it only matches the value 1:

int count = 2;

if (count) { /* runs: any nonzero value is truthy */ }
if (count == true) { /* does not run: 2 is not equal to 1 */ }

Test truthiness with if (count) or convert explicitly with bool flag = count; and then test flag. Writing == true on anything that is not already a bool is a bug waiting to happen. Writing == true on an actual bool is legal but adds nothing.

Printing a bool

There is no printf format specifier made for bool. A bool is promoted to int when passed to printf, so %d prints 0 or 1. To print words, use a conditional expression:

printf("%s\n", is_ready ? "true" : "false");

Reading a bool with scanf

There is no format specifier for reading a bool either. Read into an int and convert it:

int answer = 0;
if (scanf("%d", &answer) == 1) {
    bool yes = answer;   /* any nonzero input becomes true */
}

Checking the return value of scanf matters here; see scanf in C: syntax, return value and pitfalls for why.

Defining your own bool

Older code often defines its own type with typedef int bool; or #define bool int, or builds one from an enum such as typedef enum { false, true } bool;. Those still compile under old standards, but they conflict with <stdbool.h> if both are present, and under C23 they conflict with the keywords themselves, because you cannot redeclare a keyword. If you are modernising such code, delete the custom definition and use the standard one. For how enums and typedefs work, see enum in C: syntax and examples and typedef in C: syntax and uses.

Bool in expressions

The logical operators &&, || and ! all produce an int that is 0 or 1 in C, not a bool. That is fine in practice, because it converts back to bool without loss. The cppreference page for stdbool.h shows the idea with bool a = true, b = false;, where a && b prints 0, a || b prints 1, and !b prints 1.

Bitwise operators behave differently from logical ones. & and | on two bools first promote them to int and combine all the bits, which gives the same 0 or 1 here but does not short-circuit. When you mean "and" or "or" in a condition, use && and ||. More on the bit-level operators is in bitwise operators in C with examples.

Returning bool from a function

A function that answers a yes-or-no question reads best with a bool return type and a name that sounds like a question:

#include <stdbool.h>

bool is_even(int n)
{
    return n % 2 == 0;
}

The comparison n % 2 == 0 yields an int 0 or 1, which converts to bool on return. Callers can then write if (is_even(4)) with no comparison against true.

If it does not compile

If the compiler says that bool is an unknown type name, you almost certainly forgot #include <stdbool.h> while compiling under C99, C11 or C17. If you get a redefinition error on bool, true or false, search the project for a home-made definition, as described above, and remove it.

Frequently asked questions

Does C have a built-in bool type?
Yes. C99 added the built-in type _Bool. The name bool comes from the <stdbool.h> header, which defines bool as a macro for _Bool. Since C23, bool, true and false are keywords, so no header is needed.
What values can a bool hold in C?
Only 0 (false) and 1 (true). Assigning any other scalar value converts it first: zero becomes 0 and every nonzero value becomes 1. So a bool set from 42 holds 1.
Is true equal to 1 in C?
With <stdbool.h>, true expands to the integer constant 1 and false to the integer constant 0. Because of that, an int holding 2 is truthy in an if statement but is not equal to true.
Do I need #include <stdbool.h> in C23?
No. In C23, bool, true and false are keywords. Code that has to compile under C99, C11 or C17 still needs the header, so many projects keep the include for portability.

Sources