Skip to content
Related Articles

Related Articles

Improve Article
Bool data type in C++
  • Difficulty Level : Easy
  • Last Updated : 05 Jun, 2017

The ISO/ANSI C++ Standard has added certain new data types to the original C++ specifications.They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers.
One of the new data type is: bool
Syntax:

bool b1 = true;      // declaring a boolean variable with true value   

In C++, the data type bool has been introduced to hold a boolean value, true or false.The values true or false have been added as keywords in the C++ language.
Important Points:

  • The default numeric value of true is 1 and false is 0.
  • We can use bool type variables or values true and false in mathematical expressions also.For instance,
    int x = false + true + 6;

    is valid and the expression on right will evaluate to 7 as false has value 0 and true will have value 1.

  • It is also possible to convert implicitly the data type integers or floating point values to bool type. For example, the statements-




    bool x = 0;  // false
    bool y = 100;  // true
    bool z = 15.75;  // true




// CPP program to illustrate bool 
// data type in C++
#include<iostream>
using namespace std;
int main()
{
    int x1 = 10, x2 = 20, m = 2;
    bool b1, b2;
    b1 = x1 == x2; // false
      
    b2 = x1 < x2; // true
      
    cout << "b1 is = " << b1 << "\n";
    cout << "b2 is = " << b2 << "\n";
    bool b3 = true;
      
    if (b3)
        cout << "Yes" << "\n";
    else
        cout << "No" << "\n";
          
    int x3 = false + 5 * m - b3;
    cout << x3;
      
return 0;
      
}

Output:

b1 is = 0
b2 is = 1
Yes
9

Related article: How to use Bool in C?
 

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :