Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

bool — Boolean type

bool is a built-in C++ type with two values: false and true. It stores logical states. Converting a number to bool tests whether the number is nonzero, not whether it equals 1.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

bool name = true;
bool disabled = false;
bool value{};
bool result = (3 < 5);

bool is a distinct fundamental integer type with exactly two values: false and true. Use it to represent states such as “enabled” or “disabled”. It is not an alias for int, and the standard does not guarantee that a bool object occupies one bit or that sizeof(bool) is 1.

When an arithmetic value is converted to bool, zero becomes false and every nonzero value, including a negative value, becomes true. A null pointer value or null member pointer value becomes false; other values of those types become true. In the opposite direction, promotion from bool to int produces 0 for false and 1 for true.

In the example, enabled receives true, empty initialization of empty{} produces false, and -7 is converted to true. The static_cast<int>(...) expressions explicitly convert these values to numbers, so the expected output is 1 0 1 followed by a newline.

This entry targets C++23. The supplied site hosts an evolving working draft, not a frozen C++23 publication. The compiler flag specifies the intended verification mode; it does not attest to a production runtime.

Examples

#include <iostream>

int main() {
    bool enabled = true;
    bool empty{};
    bool fromNumber = -7;

    std::cout << static_cast<int>(enabled) << ' '
              << static_cast<int>(empty) << ' '
              << static_cast<int>(fromNumber) << '\n';
}

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