C Boolean

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value.

In C Boolean, '0' is stored as 0, and another integer is stored as 1. We do not require to use any header file to use the Boolean data type in C++, but in C, we have to use the header file, i.e., stdbool.h. If we do not use the header file, then the program will not compile.

Syntax
bool variable_name; 
In the above syntax, bool is the data type of the variable, and variable_name is the name of the variable.
#include <stdio.h>  
#include<stdbool.h>  
int main()  
{  
bool x=false; // variable initialization.  
if(x==true) // conditional statements  
{  
printf("The value of x is true");  
}  
else  
printf("The value of x is FALSE");  
return 0;  
}  
In the above code, we have used <stdbool.h> header file so that we can use the bool type variable in our program. After the declaration of the header file, we create the bool type variable 'x' and assigns a 'false' value to it. Then, we add the conditional statements, i.e., if..else, to determine whether the value of 'x' is true or not.
Output
The value of x is FALSE

Boolean with Logical Operators
The Boolean type value is associated with logical operators. There are three types of logical operators in the C language:

&&(AND Operator): It is a logical operator that takes two operands. If the value of both the operands are true, then this operator returns true otherwise false

||(OR Operator): It is a logical operator that takes two operands. If the value of both the operands is false, then it returns false otherwise true.

!(NOT Operator): It is a NOT operator that takes one operand. If the value of the operand is false, then it returns true, and if the value of the operand is true, then it returns false.
#include <stdio.h>  
#include<stdbool.h>  
int main()  
{  
bool x=false;  
bool y=true;  
printf("The value of x&&y is %d", x&&y);  
printf("\nThe value of x||y is %d", x||y);  
printf("\nThe value of !x is %d", !x);  
}  
Output
The value of x&&y is 0 
The value of x||y is 1 
The value of !x is 1 







Posted on by