`bool` — C++ Keyword
`bool` — C++ Keyword
The bool keyword in C++: the Boolean type holding true or false.
`bool` — C++ Keyword
The bool keyword in C++: the Boolean type holding true or false.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
boolThe Boolean type. A bool variable holds either true or false. Any non-zero value converts to true; zero converts to false.
bool name;
bool name = true;
bool name = expression; // any expression convertible to bool
#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
}
sizeof(bool) is implementation-defined but typically 1.std::boolalpha causes true/false to print as text rather than 1/0; std::println handles bool as text by default.bool pre-dates C++, but C gained _Bool in C99 (and bool as a macro via <stdbool.h>).boolint 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;
}