C Boolean Type: _Bool, bool, true, and false

C boolean type diagram showing _Bool, bool, true, and false

The C boolean type is based on the built-in _Bool type. A Boolean object stores either 0 or 1, although C conditions can evaluate any integer or pointer value as false or true. The value 0 and a null pointer are false; nonzero integers and non-null pointers are true.

For C99 through C17 code, stdbool.h supplies the familiar bool, true, and false names. Use either the built-in type or the header-provided names consistently within a project.

The C boolean type: _Bool stores normalized 0 or 1

_Bool is a built-in type in C99 and later. When an integer, pointer comparison, or other scalar value is assigned to _Bool, C converts it to a normalized Boolean value: zero becomes 0, and any nonzero value becomes 1.

This complete program assigns both zero and a nonzero integer to _Bool variables:

  • #include <stdio.h>
  • int main(void) {
  • _Bool enabled = 7;
  • _Bool disabled = 0;
  • printf(“%d %d\n”, (int)enabled, (int)disabled);
  • return 0;
  • }

The output is 1 0. The explicit casts make the intended integer output clear. A _Bool value also undergoes integer promotion when passed to a variadic function such as printf, but the cast is a useful portable and readable pattern.

The C bool type: stdbool.h aliases and complete declarations

The C bool type is available through the standard header stdbool.h. In C99 through C17, that header defines bool as an alias-like macro for _Bool, while true and false represent 1 and 0. The header lets declarations read naturally without requiring a C++-style built-in bool keyword.

This complete example declares and prints two Boolean values:

  • #include <stdbool.h>
  • #include <stdio.h>
  • int main(void) {
  • bool valid = true;
  • bool complete = false;
  • printf(“%s %s\n”, valid ? “true” : “false”, complete ? “true” : “false”);
  • return 0;
  • }

Include stdbool.h before using bool, true, or false. Without the header, those names are not portable in traditional C versions that provide them as macros.

How the bool type in C handles conditions and assignments

The bool type in C is useful for storing a condition’s result, but an if statement does not require a Boolean object. C evaluates the controlling expression directly: integer zero is false, every nonzero integer is true, a null pointer is false, and every non-null pointer is true.

For example, if (count) enters its block when count is nonzero. Similarly, if (buffer) enters its block when buffer points to an object. To normalize either value for storage, assign it to bool:

  • bool has_items = count;
  • bool has_buffer = buffer;

Both assignments store only true or false. Comparisons and logical operators produce an integer result of 0 or 1, which can also be assigned directly to bool.

Boolean functions, return values, and portable output

A Boolean function should include stdbool.h and return bool when callers need a true-or-false result. A comparison such as n % 2 == 0 produces 1 or 0, and returning it from a bool function makes the interface explicit.

  • #include <stdbool.h>
  • #include <stdio.h>
  • bool is_even(int n) { return n % 2 == 0; }
  • int main(void) {
  • printf(“%s\n”, is_even(8) ? “true” : “false”);
  • return 0;
  • }

The conditional operator converts the Boolean result into one of two string literals, making %s a portable way to print the words true and false. For numeric output, use printf(“%d\n”, (int)is_even(8)) to print 1 or 0.