Understanding of Boolean in C Language
In C, Boolean is a data type exclusively containing two values, namely 0 and 1. Essentially, the bool type signifi
es two distinct behaviors: true or false. Here, ‘0’ signifies false, while ‘1’ represents true.In C Boolean, ‘0’ translates to 0 in storage, while any other integer equates to 1. Utilizing the Boolean data type in C mandates the inclusion of the stdbool.h header file, unlike C++, where no such requirement exists. Failure to include this header file will result in program compilation errors.
Syntax:
bool variable_name;
In the aforementioned syntax, bool is the variable’s data type, and variable_name is the variable’s identifier.
Let’s grasp this concept through an illustrative example.
#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 include the header file to enable the use of the bool type variable in our program. Following the header file declaration, we create the bool type variable ‘x’ and assign the value ‘false’ to it. Subsequently, we employ conditional statements, i.e., if…else, to ascertain whether the value of ‘x’ is true or not.
Output:
The value of x is FALSE
Boolean Array
Next, we instantiate a bool type array. A Boolean array can exclusively contain either true or false values, and access to the array’s values is facilitated through indexing.
Let’s illustrate this scenario with 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 declare a Boolean type array containing two values, i.e., true and false.
Output:
1,0,
Typedef
There exists an alternate approach for utilizing Boolean values, known as typedef. Essentially, typedef is a C language keyword employed to assign a name to a preexisting data type.
Let’s examine a simple example of typedef.
#include <stdio.h>
typedef enum { false, true } b;
int main()
{
b 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 employ Boolean values, namely true and false, without utilizing the bool type. We achieve this by creating a new name for the ‘bool’ type using the typedef keyword in the program.
typedef enum { false, true } b;
The statement above generates a new name ‘b’ for the ‘bool’ type, allowing ‘b’ to exclusively contain either true or false values. We use the ‘b’ type in our program and instantiate the ‘x’ variable of type ‘b’.
Output:
The value of x is false
Boolean with Logical Operators
Boolean type values are closely associated with logical operators in the C language. There are three types of logical operators:
&&(AND Operator): This logical operator takes two operands. It returns true only if both operands are true, otherwise, it returns false.
||(OR Operator): The OR operator also requires two operands. It returns true if at least one of the operands is true, otherwise, it returns false.
!(NOT Operator): The NOT operator takes a single operand and returns true if the operand is false, and false if the operand is true.
Let’s comprehend this concept 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("\nThe value of !x is %d", !x);
return 0;
}
Output:
The value of x && y is 0
The value of x || y is 1
The value of !x is 1
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.