Home > Blogs > 30 C++ Tips in 30 Days: Tip # 11 displaying literal boolean values

30 C++ Tips in 30 Days: Tip # 11 displaying literal boolean values

By  Nov 29, 2007

Topics: Programming, C/C++

By default, iostream objects display bool variables as 0 and 1. Learn how to override this setting.

Inserting the format flag boolalpha to the object stream will force iostream objects to display the literal names instead:

#include <iostream>
using namespace std;
int main()
{   
 bool b = true;
 cout<<b;  // default setting; output 1
 cout<<boolalpha;  //henceforth,display 'true' and 'false'
 cout<<b;   
 cout<<!b;   

The symbols true and false are locale-dependent, so in a different locale they will be replaced with that locale's equivalents.

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.