In C, the boolean type is a data type that contains two values, namely 0 and 1. Essentially, the value of the bool type represents two behaviors, namely true or false. Here, '0' represents the false value, while '1' represents the true value.
In C, '0' is stored as 0, while other integers are stored as 1. In C++, we do not need to use any header files to use the boolean data type, but in C, we must use the header file 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, and variable_name is the name of the variable.
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
#include <stdio.h> #include<stdbool.h> int main() { bool x=false; // Variable initialization if(x==true)// Conditional statement{ printf("x's value is true"); } else printf("x's value is false"); return 0; }
Output
x's value is false
Boolean Array Now, we create an array of bool type. A boolean array can contain true or false values and can access the values of the array by index.
Let’s understand this situation 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; }
Output
1,0,
typedef The keyword typedef in C is used to give a name to an existing data type.
Let’s look at a simple example of typedef.
#include <stdio.h> typedef enum {false, true} b; int main(){ b x = false; // Variable initialization if (x == true) // Conditional statement { printf("x's value is true"); } else { printf("x's value is false"); } return 0;}
In the above code, we used the boolean values true and false, but did not use the bool type. We used boolean values by creating a new name for the 'bool' type. To achieve this, we use the typedef keyword in the program.
typedef enum {false, true} b;
The above statement creates a new name 'b' as an alias for the 'bool' type. We use the 'b' type in the program and create a variable 'x' of type 'b'.
Output
x's value is false
Boolean Type with Logical Operators Boolean type values are associated with logical operators. There are three logical operators in C:
&& (AND operator): It is a logical operator that takes two operands. If both operands are true, this operator returns true; otherwise, it returns false.
|| (OR operator): It is a logical operator that takes two operands. If both operands are false, it returns false; otherwise, it returns true.
! (NOT operator): It is a NOT operator that takes one operand. If the operand is false, it returns true; if the operand is true, it returns false.
Let’s understand this through an example.
#include <stdio.h> #include <stdbool.h> int main(){ bool x = false; bool y = true; printf("x&&y's value is%d\n", x && y); printf("x||y's value is%d\n", x || y); printf("!x's value is%d\n", !x); return 0;}
Output
x&&y's value is 0 x||y's value is 1 !x's value is 1
Programmer Technical Exchange Group
Scan the code to join the group, remember to note: city, nickname, and technical direction.