C++ boolalpha Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, boolalpha is a manipulator that changes how a bool is shown. Normally cout << true prints 1. After boolalpha it prints the word true.

It also works for input: with boolalpha on, cin >> flag accepts the words true and false. The setting stays until you turn it off with noboolalpha. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::boolalpha into the stream before the bool values.

C++
std::cout << std::boolalpha << true;     // true
std::cin >> std::boolalpha >> flag;       // reads the word true or false
CallWhat it does
std::cout << std::boolalphaPrints bools as words from now on.
std::cout << std::boolalpha << (5 > 3)Prints true for a comparison result.
std::cin >> std::boolalpha >> flagReads the words true or false into a bool.

Only bool values are affected. An int such as 1 is still printed as 1.

Library declarations
C++
ios_base& boolalpha(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << boolalpha. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after boolalpha.

C++ Examples

Example 1 — Print true and false as Words

Compare the output before and after boolalpha.

C++
#include <iostream>

int main() {
    bool yes = true;
    bool no = false;

    std::cout << yes << ' ' << no << '\n';
    std::cout << std::boolalpha;
    std::cout << yes << ' ' << no << '\n';
    return 0;
}

How It Works

  1. By default a bool is printed as a number: 1 for true and 0 for false.
  2. std::cout << std::boolalpha prints nothing, but it switches the stream to word mode.
  3. The second line prints the same two variables as the words true and false.

Example 2 — Show the Result of a Comparison

A comparison gives a bool, so boolalpha makes the answer easy to read.

C++
#include <iostream>

int main() {
    int age = 20;

    std::cout << std::boolalpha;
    std::cout << "adult: " << (age >= 18) << '\n';
    std::cout << "senior: " << (age >= 65) << '\n';
    return 0;
}

How It Works

  1. age >= 18 is a comparison. Its result is a bool, here true.
  2. The parentheses are needed, because << would otherwise be evaluated first.
  3. With boolalpha already switched on, the answers appear as words without any extra code.

Example 3 — Read true and false from Text

Reading a word into a bool needs boolalpha. Without it, the read fails.

C++
#include <iostream>
#include <sstream>

int main() {
    bool x = false;
    std::istringstream a("true");
    a >> std::boolalpha >> x;
    std::cout << std::boolalpha << "x = " << x << '\n';

    bool y = false;
    std::istringstream b("true");
    b >> y;
    std::cout << "plain read failed: " << (b.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. a >> std::boolalpha >> x understands the word true and stores true in x.
  2. The plain read b >> y expects the digit 0 or 1. The letter t is not valid, so the stream fails.
  3. y was already false, and the failed read does not give it a useful value.

Frequently Asked Questions

It makes a stream print bool values as the words true and false instead of 1 and 0, and read those words on input.
Use std::noboolalpha. It returns the stream to printing 1 and 0.
No. Only values of type bool are affected. An int 1 is still printed as 1.
Without boolalpha, cin expects 0 or 1 for a bool. Add std::boolalpha before the read, or type 1 and 0.
It stays on for that stream until you turn it off or the program ends. It does not reset after one value, unlike setw.

Next: dec

Read and print integers in base 10.

dec tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful