| Boolean in C
When we say about the Boolean in C, its a data type which contains two types of values either 0 or 1. And bool type value represents two types of behavior, which can be true or false. And here "0" represent false and "1" represents true value.
In C Boolean, "0" is stored as 0 and another integer is stored as 1. There is no need to add any header files to use the Boolean data type in C++, but in C we have to add stdbool.h. And if we don not use header file we will get compile time error and program will not compile.
Syntax to use bool variable is given below
bool variable_name;
As per the above syntax, bool is data type of variable,and variable_name is the name of the variable.
As per the above code we have used to use bool type variable in our code. And afetr declartion of the header file, we have create the bool type variable "A" and assign "false" value to it. And we have added the conditional statements to determine the value of "A" is true or not.
And output of above code is
The value of A is False
Boolean Array
We can also create a bool type array. And this Boolean array can contain either true or false, and value of the array can be accessed with the help of indexing.
As per above code we have declared a Boolean type array which contain tow value true or false.
And output of above code is
1,0,1,0,0,
typedef
We can use Boolean value in another way too and that is typedef. And basically typedef is one of the keyword in C language, which is used to assign the name to already existing datatype.
Below is the example of typedef
As per above code, we us the Boolean values true or false. But we have not used the bool type. We use the Boolean values by creating a new name of the "bool" type. In order to achieve this, the typedef keyword is used in the program.
typedef enum{false,true} A;
As the above statement creates a new name for "bool" type. "A" can contain either true or false value. Here in above code we use "A" type in our program and create "x" variable of type "A".
And output of above code is
The value of x is false
Boolean with Logical Operators
When we say about the boolean value it is associated with logical operators. And we have 3 types of logical operators in C.
(1)&&(AND Operator):-This is a logical operator which takes two operands. And if value of both operands are true, then this operator returns true otherwise false.
(2)||(OR Operator):-This is logical operator which also takes two operands and if both operands is false, then it returns false otherwise true.
(3)!(NOT Operator):-It is Not operator which take one operand. And if value of the oeprand is false, then it returns true. And if the value of the operand is true, then it returns false.
Below example will helps you to understand more on it:-
and output of above code is :-
| | |