Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.03 KB

CppBool.md

File metadata and controls

53 lines (38 loc) · 1.03 KB

bool is the keyword for a standard data type that can store the truth values true and false.

Example: simple use of bool

#include <iostream>

int main() 
{   
  const bool a = true;   
  const bool b = false;
  std::cout << a << ' ' << b << '\n'; 
}

This will display:

1 0

To show the words 'true' and 'false', use std::boolalpha:

#include <iomanip>
#include <iostream>

int main() 
{   
  const bool a = true;   
  const bool b = false;
  std::cout << std::boolalpha << a << ' ' << b << '\n'; 
}

This will display:

true false
  • Always use bool for logical expressions [1]
  1. Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 1.3.2: 'Always use bool for logical expressions'