Home » C++ programming language

What is bool and Boolean literals in C++?



What is bool?

C++ introduced a new type of Data Type named bool which stands for Boolean. This data type is introduced to support true or false value that means we can store either true or false values. We can also store 0 as false or 1 as true.

bool data type occupies only 1 Byte in the memory.

Syntax

bool variable_name=boolean_value;
bool variable_name=0/1;

What are Boolean Literals?

Boolean literals are true and false, these are two keywords added in C++ programming. Here true represents for 1 and false represents 0.

If we try to print the values of true and false, values will be 1 and 0 respectively, let's consider the following statements:

cout<<"value of true is: " <<true <<endl;
cout<<"value of false is: "<<false<<endl;

Output

value of true is: 1 
value of false is: 0

Here is an example, in which we are assigning false, true and 0 in the variable marital_status

#include <iostream>
using namespace std;
int main()
{
	//assigning false
	bool marital_status=false;
	
	cout<<"Type1..."<<endl;
	if(marital_status==false)
	    cout<<"You're unmarried"<<endl;
	else
	    cout<<"You're married"<<endl;

	//assigning true
	marital_status=true;
	cout<<"Type2..."<<endl;
	if(marital_status==false)
	    cout<<"You're unmarried"<<endl;
	else
	    cout<<"You're married"<<endl;

	//now assigning 0 as false
	marital_status=0;
	cout<<"Type3..."<<endl;
	if(marital_status==false)
	    cout<<"You're unmarried"<<endl;
	else
	    cout<<"You're married"<<endl;	
	
	return 0;	
}

Output

Type1...
You're unmarried
Type2...
You're married
Type3...
You're unmarried





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.