`true` — C++ Keyword

`true` — C++ Keyword

The true keyword in C++: the Boolean literal for the true value.

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.

true

The Boolean literal representing the value 1 (logically true). Has type bool.

Syntax

bool b = true;

Example

#include <print>

int main() {
    bool flag = true;

    std::println("{}", flag);             // true
    std::println("{}", static_cast<int>(true));  // 1

    // In conditions
    if (true) {
        std::println("always runs");
    }

    // Comparison
    std::println("{}", (1 == 1) == true);    // true
    std::println("{}", (1 != 2));            // true
}

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;
}