C Boolean

What is Boolean in C programming?

In C programming, Boolean is a data type which contains two types of values, i.e., 0 and 1. These two values are binary values.

Basically, the value of bool type represents two types of behavior, either true or false. Here, ‘1’ represents the true value, while ‘0’ represents the false value.

In C Boolean, ‘0’ is stored as 0, while the another integer is stored as 1. We do not need to use any header file to use the Boolean data type in C ++. But in C, we have to use a header file, that is, stdbool.h. If we do not use the header file, the program will not compile.

Syntax:-
bool variable_name; 

In the above syntax, bool is the data type of the variable, & variable_name is the name of the variable.

C Boolean
What is Boolean in C programming
Example:

Let’s understand through an example.

#include <stdio.h> 
 #include<stdbool.h> 
 int main()  
 {  
 bool x=false; // variable initialization.  
 printf("The value of x is FALSE ");  
 printf("%d",x);
 return 0;  
 }  

In the above program, we have used stdbool.h header file. We used it, so that we can use bool type variable in our programs.

After the declaration of the header file, we create a bool type variable ‘x’ and assign it a ‘false’ value. Then, we used the printf statement twice.

In the first case, we printed a simple statement. In the second statement, we printed the value of x.

Boolean Array

Now, we create a bool type array. Boolean arrays can contain true or false values, and the array’s values ​​can be accessed with the help of indexing.

Example:

Let us understand this concept through an example.

#include <stdio.h>  
#include<stdbool.h> 
 int main()  
 {  
 bool b[2]={true,false}; // Boolean type array  
 for(int i=0;i<2;i++) // for loop  
 {  
 printf("%d,",b[i]); // printf statement  
 }  
 return 0;  
 }  

In the above code, we have declared an array of Boolean type that has two values, i.e., true and false.

typedef

There is another way of using Boolean value, i.e., typedef. Basically, typedef is a keyword in C language, which is used to assign the name to the already existing datatype.

Example:

Let’s see a simple example of typedef.

#include <stdio.h>  
 typedef int ram;  
 int main()  
 {  
 ram x;
 printf("Enter a number \n");
 scanf("%d\n",&x);
 printf("The entered value is %d",x);
 return 0;  
 }  

In the above code, we use the Boolean values, i.e., true and 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 int ram;

The above statement creates a new name for the ‘bool’ type, i.e., we used ram instead of int keyword. We use ‘ram’ type in our program and print a entered value.

Boolean with Logical Operators

Boolean type values ​​are associated with logical operators. There are three types of logical operators in 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.

Example:

Let’s understand through an example.

#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("\nValue of !x is %d", !x);  
 }  
c Boolean

Leave a Comment