Understanding Boolean Values
Boolean values are the building blocks of logical operations, enabling programmers to express and manipulate true/false conditions in their code.
Boolean variables
Boolean variables are variables that can have only two possible values: true, and false.
To declare a Boolean variable, we use the keyword bool. It can take one of two values: true or false.
bool boolUninitialized; // uninitialized, default to false
bool boolInitialized = true; // initializedBoolean values are not actually stored in Boolean variables as the words true or false. Instead, they are stored as integers: true becomes the integer 1, and false becomes the integer 0. Similarly, when Boolean values are evaluated, they don't actually evaluate to true or false. They evaluate to the integers 0 (false) or 1(true). Because Booleans actually store integers, they are considered as integral type.
Printing Boolean values
When we print Boolean values, std::cout prints 0 for false and 1 for true:
#include <iostream>
int main()
{
std::cout << true << '\n'; // true evaluates to 1
std::cout << !true << '\n'; // !true evaluates to 0
bool b {false};
std::cout << b << '\n'; // b is false, which evaluates to 0
std::cout << !b << '\n'; // !b is true, which evaluates to 1
return 0;
}
// Output
1
0
0
1If you want std::cout to print true or false instead of 0 or 1, you can use std::boolalpha Here's an example:
#include <iostream>
int main()
{
std::cout << true << '\n';
std::cout << false << '\n';
std::cout << std::boolalpha; // print bools as true or false
std::cout << true << '\n';
std::cout << false << '\n';
return 0;
}
// Output
1
0
true
falseYou can use std::noboolalpha to turn it back off.
Integer to Boolean conversion
When using uniform initialization, you can initialize a variable using integer literals 0 (for false) and 1 (for true) but your really should be using false and true instead. Other integer literals cause compilation errors:
#include <iostream>
int main()
{
bool bFalse { 0 }; // okay: initialized to false
bool bTrue { 1 }; // okay: initialized to true
bool bNo { 2 }; // error: narrowing conversions disallowed
std::cout << bFalse << bTrue << bNo << '\n';
return 0;
}However, in any other context where an integer can be converted to a Boolean, the integer 0 is converted to false, and other integer is converted to true.
#include <iostream>
int main()
{
std::cout << std::boolalpha; // print bools as true or false
bool b1 = 4 ; // copy initialization allows implicit conversion from int to bool
std::cout << b1 << '\n';
bool b2 = 0 ; // copy initialization allows implicit conversion from int to bool
std::cout << b2 << '\n';
return 0;
}
// Output
true
falseNote: bool b1 = 4; may generate a warning. If so you will have to disable treating warnings as errors to compile the example.
Inputting Boolean values
Inputting Boolean values using std::cin sometimes trips new programmers up.
Consider the following example:
#include <iostream>
int main()
{
bool b{}; // default initialize to false
std::cout << "Enter a boolean value: ";
std::cin >> b;
std::cout << "You entered: " << b << '\n';
return 0;
}Enter a Boolean value: true You entered: 0
What just happened here?
It turns out that std::cin only accepts two inputs for Boolean variables: 0 and 1. (not true or false). Any other inputs will cause std::cin to silently fail. In this case, because we entered true, std::cin silently failed. A failed input will also zero-out the variable, so b also gets assigned value false. Consequently, when std::cout prints a value for b, it prints 0.
To allow std::cin to accept false and true as inputs, the std::boolalpha options has to be enabled:
#include <iostream>
int main()
{
bool b{};
std::cout << "Enter a boolean value: ";
// Allow the user to enter 'true' or 'false' for boolean values
// This is case-sensitive, so True or TRUE will not work
std::cin >> std::boolalpha;
std::cin >> b;
std::cout << "You entered: " << b << '\n';
return 0;
}However, when std::boolalpha is enabled, 0 and 1 will no longer be interpreted as Booleans inputs. (they both resolve to “false" as does any non-true input).
Leave a comment
Your email address will not be published. Required fields are marked *


