Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

bool — Boolean type

In C23, bool is a built-in Boolean type. It stores false or true; converting a nonzero number produces true rather than preserving the original value.

Version: C23; repository compiler flag, production runtime not attested

Syntax

bool name = true;
// Or, instead of the definition above:
// bool name = false;

bool is a C23 keyword; true and false require no <stdbool.h> header. The older spelling _Bool is deprecated.

Conversion to bool maps arithmetic zero, a null pointer, and a value of type nullptr_t to false; other scalar values become true. Assigning -7 therefore stores a Boolean value, not the number -7.

Arguments of type bool passed to printf are promoted to int, so %d prints 0 or 1. The example expects 1 0 1.

Examples

#include <stdio.h>

int main(void)
{
    bool ready = true;
    bool empty = 0;
    bool active = -7;

    printf("%d %d %d\n", ready, empty, active);
    return 0;
}

Result

1 0 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources