`bool` — C++ Keyword

`bool` — C++ Keyword

The bool keyword in C++: the Boolean type holding true or false.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

bool

The Boolean type. A bool variable holds either true or false. Any non-zero value converts to true; zero converts to false.

Syntax

bool name;
bool name = true;
bool name = expression;   // any expression convertible to bool

Example

#include <print>

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

int main() {
    bool a = true;
    bool b = false;

    std::println("{} {}", a, b);           // true false (with std::boolalpha)
    std::println("{}", is_even(4));        // true
    std::println("{}", is_even(7));        // false

    // Numeric context: bool promotes to int (0 or 1)
    std::println("{}", static_cast<int>(a) + static_cast<int>(b)); // 1
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}