Language[lang]

bool in C++: true, false and how to use them

Section
Language [lang]
Standard
C++17
Compilers
MSVC 19.44, Clang 19.1 (MSVC STL), GCC 16.2, Clang 23.1 (libc++)
Published
Length
7 min read
In this answer6 clauses
  1. 1What produces a bool in C++?
  2. 1.1Do && and || always run both sides?
  3. 2Why does cout print a bool as 1 or 0?
  4. 3What converts to bool, and what does a bool turn into?
  5. 4How do you write a bool function in C++?
  6. 5Is it bool or boolean in C++?
  7. 6Common bool mistakes

Abstract

A bool in C++ is a built-in type with two values, true and false. Comparisons such as age >= 18 and the operators &&, || and ! produce one, and an if tests one. A number converts to false when it is 0 and to true otherwise, a null pointer to false, and true becomes 1, false 0. std::cout prints a bool as 1 or 0 unless std::boolalpha is sent first.

A slide switch drawn as a long rounded frame with room for a knob at each end. The left position holds a grey hatched circle, the place the knob left; an arrow leads right to a solid blue knob.
Figure 1 — Two values and nothing else.

A bool in C++ is a built-in type with two values, true and false. Declare a bool variable with bool done = false; and use it wherever a condition goes: if (done). Comparisons such as age >= 18 produce a bool, and std::cout prints one as 1 or 0 unless you send std::boolalpha first.

A ticket check, as a whole program:

[Example 1:
#include <iostream>

int main() {
    int age = 20;
    bool is_adult = age >= 18;  // a comparison gives a bool

    if (is_adult) {
        std::cout << "Adult ticket\n";
    } else {
        std::cout << "Child ticket\n";
    }
    std::cout << "is_adult = " << is_adult << '\n';  // prints 1
}

— end example]

It printed:

[Example 2:
Adult ticket
is_adult = 1

— end example]

age >= 18 asks a question, and its answer, true for an age of 20, is stored in is_adult.

The samples are C++17, built with MSVC 19.44, Clang 19.1 (MSVC STL), GCC 16.2 and Clang 23.1 (libc++), and all four printed the same. bool, true and false go back to the first standard, C++98 (Since When lists the compiler releases for each standard from C++11 on).

What produces a bool in C++?

Comparisons produce one: == (equal), != (not equal), <, >, <= and >=. The logical operators combine bools into a new one: && (and), || (or) and ! (not) (logical operators on cppreference). On the ticket check:

[Example 3:
int age = 20;
bool has_ticket = false;

std::cout << std::boolalpha;
std::cout << (age == 20) << '\n';                // true
std::cout << (age < 18) << '\n';                 // false
std::cout << (age >= 18 && has_ticket) << '\n';  // false
std::cout << (age >= 18 || has_ticket) << '\n';  // true
std::cout << !has_ticket << '\n';                // true

// the same operators spelled as words
std::cout << (age >= 18 and not has_ticket) << '\n';  // true
std::cout << (age < 18 or has_ticket) << '\n';        // false

— end example]

Each line printed the word in its comment. and, or and not are the same operators spelled as words, part of the language, with no header to include; MSVC accepts them only in its standard-conforming mode, /permissive-, which /std:c++20 and later turn on (Microsoft Learn on &&). For two bools a and b:

a b a && b a || b
false false false false
false true false true
true false false true
true true true true

!a is true when a is false, and false when it is true.

Do && and || always run both sides?

No. && stops when its left side is false, and || stops when its left side is true, because the answer is already known; the right side then never runs. This is called short-circuit evaluation:

[Example 4:
bool check_ticket() {
    std::cout << "check_ticket() called\n";
    return true;
}
// ...
bool is_adult = false;
bool is_staff = true;

if (is_adult && check_ticket()) {  // is_adult is false: no call
    std::cout << "adult with a ticket\n";
}
if (is_staff || check_ticket()) {  // is_staff is true: no call
    std::cout << "let in\n";
}

if (is_staff && check_ticket()) {  // is_staff is true: called
    std::cout << "staff with a ticket\n";
}

— end example]

It printed:

[Example 5:
let in
check_ticket() called
staff with a ticket

— end example]

The first two ifs never called check_ticket(); the third did, because a true on the left of && leaves the answer open. So a function on the right of && or || may not run at all: do not count on what it does, such as the line it prints.

Why does cout print a bool as 1 or 0?

Because digits are the default: a stream prints true as 1 and false as 0 until you send it std::boolalpha, which switches it to the words (std::boolalpha on cppreference).

[Example 6:
bool is_adult = true;
bool has_ticket = false;

std::cout << is_adult << ' ' << has_ticket << '\n';  // 1 0
std::cout << std::boolalpha;  // from here on: words
std::cout << is_adult << ' ' << has_ticket << '\n';  // true false

std::cout << std::noboolalpha;  // back to numbers
std::cout << is_adult << '\n';  // 1

— end example]

It printed:

[Example 7:
1 0
true false
1

— end example]

The setting stays on std::cout for every bool printed after it, until std::noboolalpha turns it off; how to print with std::cout covers the stream’s other settings, such as decimals. Sent to std::cin, it does the same for input: the stream reads the words true and false instead of 1 and 0. To print the word once without changing the stream, pick it with the ternary operator, parentheses included: std::cout << (is_adult ? "true" : "false"); (without them << runs first and the line prints 1).

What converts to bool, and what does a bool turn into?

A number converts to false when it is 0 and to true when it is anything else, negative numbers included, and a null pointer converts to false (implicit conversions on cppreference). The other way, true becomes 1 and false becomes 0:

[Example 8:
int zero = 0;
int five = 5;
int minus_one = -1;
double nothing = 0.0;
int* seat = nullptr;

bool a = zero;       // false
bool b = five;       // true
bool c = minus_one;  // true: any number but 0
bool d = nothing;    // false
bool e = seat;       // false: a null pointer

std::cout << std::boolalpha;
std::cout << a << ' ' << b << ' ' << c << ' '
          << d << ' ' << e << '\n';

int one = true;    // 1
int none = false;  // 0
std::cout << one << ' ' << none << '\n';
std::cout << true + true << '\n';  // 2: an int, not a bool

— end example]

It printed:

[Example 9:
false true true false false
1 0
2

— end example]

Two panels of arrows. The first, to bool: 0 becomes false, 5 becomes true, -1 becomes true and a null pointer becomes false. The second, from bool: true becomes 1 and false becomes 0.
Figure 2 — Zero and a null pointer become false, any other value true; true becomes 1 and false 0.

true + true is not true: both sides become the int 1 before the addition, so the result is the int 2, printed as 2 even with std::boolalpha set. The same rule decides a condition, so if (seat) takes its branch only when the pointer is not null.

How do you write a bool function in C++?

Give it the return type bool and return the condition itself. Name it as a yes-or-no question, such as is_even, so that the call reads as one inside an if:

[Example 10:
bool is_even(int n) {
    return n % 2 == 0;
}
// ...
int tickets = 4;
if (is_even(tickets)) {
    std::cout << tickets << " tickets: an even number\n";
}
if (!is_even(7)) {
    std::cout << "7 is odd\n";
}

— end example]

It printed 4 tickets: an even number and 7 is odd. n % 2 is the remainder after dividing by 2, so n % 2 == 0 is already the answer: a bool function returns it, with no if that returns true in one branch and false in the other. ! turns the question around: !is_even(7) is true.

Is it bool or boolean in C++?

It is bool. C++ has no type named boolean, and bool is a keyword, so it needs no header (fundamental types on cppreference). Writing boolean is an error:

[Example 11:
boolean ready = true;  // error: C++ has no type named boolean

— end example]

All four compilers rejected it; GCC said:

[Example 12:
error: 'boolean' was not declared in this scope; did you mean 'bool'?

— end example]

Common bool mistakes

Each with its fix:

  • if (done = true) assigns instead of comparing: = stores true in done, so the test is always true, and the branch was taken. All four compilers warned (MSVC C4706, “assignment used as a condition”, at /W4; GCC and Clang under -Wparentheses, with -Wall -Wextra). Write if (done).
  • if (is_adult == true) compares a value that is already a bool; if (is_adult) says the same thing.
  • A local bool ok; with no value is not set to anything and must not be read. Write bool ok{}; (empty braces give false) or bool ok = false;.
  • A single & is the bitwise and: it always runs both sides, and on numbers it combines bits, so with 2 and 1 tickets & seats is 0 while tickets && seats is true. Between two conditions write &&.
  • std::vector<bool> is a special case: it may pack its values into less space, and v[i] returns a stand-in object (a proxy), not a reference to a bool, so it does not behave like other vectors (std::vector of bool on cppreference).

The Language section has the other answers on the core language.

Questions

What is the size of a bool in C++?

sizeof(bool) printed 1 on all four compilers used here, MSVC 19.44, GCC 16.2 and two builds of Clang, so a bool took one byte on each. The standard does not fix the size: it leaves it to the implementation and notes that sizeof(bool) is not required to be 1.

What is the default value of a bool in C++?

A local bool declared with no value, as in bool ok;, is not set to anything, and its value must not be read; a bool declared outside any function, or static, starts as false. Give a local one a value: bool ok{}; and bool ok = false; both printed false on all four compilers used here.

How do you flip a bool in C++?

Assign it its own opposite with the not operator: ok = !ok;. The ! operator gives true for false and false for true, so a bool that held false printed true after the flip.

How do you print a bool as true or false?

Send std::boolalpha to the stream first. After std::cout << std::boolalpha;, every bool printed on std::cout shows as true or false instead of 1 or 0, until std::noboolalpha switches it back to digits.

How do you convert a bool to a string in C++?

Pick one of two words with the ternary operator: std::string text = flag ? “true” : “false”; holds “true” when flag is true and “false” when it is false. Unlike std::boolalpha, it leaves the stream’s settings alone.

Can you do arithmetic with a bool?

Yes, but the bool converts to an int first: true becomes 1 and false 0, so true + true is the int 2, printed as 2 even with std::boolalpha set. The exception is ++: since C++17, ++flag on a bool does not compile. Either way, it is rarely what you want.

Sources

  1. Fundamental typescppreference.com
  2. Logical operatorscppreference.com
  3. Implicit conversionscppreference.com
  4. std::boolalpha, std::noboolalphacppreference.com
  5. [basic.fundamental] Fundamental typesC++ standard working draft
  6. [expr.sizeof] SizeofC++ standard working draft

Read next

  1. Data types in C++: int, double, char, bool and moreLanguage · Miriam Castellanos · written for C++17
  2. const in C++: variables, pointers, references, methodsLanguage · Miriam Castellanos · written for C++17
  3. assert in C++: how assert and static_assert workLanguage · Miriam Castellanos · written for C++17