C - Data Type bool Type

Introduction

The type _Bool stores Boolean values.

A Boolean value may be true or false.

The value of a variable of type _Bool can be either 0 or 1, corresponding to the Boolean values false and true, respectively.

type _Bool is regarded as an integer type.

You declare a _Bool variable. For example:

_Bool valid = 1; // Boolean variable initialized to true

To use bool as the type name, add an #include <stdbool.h> to your source file.

stdbool.h defines bool to be the equivalent of _Bool and the header file also defines the symbols true and false to correspond to 1 and 0, respectively.

You can rewrite the previous declaration as the following:

bool valid = true; // Boolean variable initialized to true

You can cast between Boolean values and other numeric types.

A nonzero numeric value will result in 1 (true) when cast to type bool, and 0 will cast to 0 (false).

If you use a bool variable in an arithmetic expression, the compiler will insert an implicit conversion where necessary.