Thursday, September 15, 2016

bool Data Type in C++

While making decisions, it is human to think in terms of true or false. In C Programming language false is represented by Zero value and true by Non-Zero. C++ introduced bool data type to hold true or false values.
Code example :
#include <iostream>
using namespace std;
int main()
{
    int x=10,y=20;
    bool b=x>y;
    cout<<" x> y = "<<b<<endl;
    return 0;
}

On execution :




Even though  we can use keywords : true or false for assigning purposes to bool variable, the values stored in a bool variable for true is 1 and false is 0.

bool b=true;
cout<<b<<endl;

The outcome is 1.

std::boolalpha function can be used for text values for bool type.  With boolalpha, cout statement will look like :
    cout<<" x> y = "<<std::boolalpha<<b<<endl; 
On execution :



boolalpha is a format flag...

No comments:

Post a Comment