Hey, folks! In this article, we will focusing on an important topic of C programming — Boolean in C.
We often come across situations wherein we need to display the outcome of any action or activity in terms of logical propositions or statements such as ‘YES‘ or ‘NO‘.
In the programming scenarios, we can achieve the same through Boolean data type in C programming.
So, let us begin.
Table of Contents
What is Boolean in C?
Boolean
is a built in data type that stores logically interpreted values into it. That is, it stores values in terms of TRUE
and FALSE
. These values are known as boolean values.
Boolean type represents the behavior of the data values in the following types:
- TRUE – corresponds to 1 as the value of true.
- FALSE – corresponds to 0 as the value of false.
Now, let us focus on the declaration and implementation of Boolean data type in the upcoming section.
Syntax of Boolean data type
Have a look at the below code to understand the declaration of boolean type variables:
bool variable;
It uses bool keyword to declare the variables of boolean data type in C.
To initialize the boolean variables, following syntax can be implemented–
bool variable = value;
Here, the boolean variable can have either of the below as values:
- TRUE
- FALSE
Further, the data values of type integer or float can be implicitly be manipulated to bool type as shown below–
bool var1 = 0; bool var2 = 155.45; bool var3 = 12;
Here, var2 and var3 would evaluate to TRUE and var1 would evaluate to FALSE.
Examples of Boolean data type in C
In the below example, we have set the value of bool variable var1 to TRUE. Moreover, the if statement used below compares the value of var1 with 1 and returns a value based upon it through printf() function.
#include <stdio.h> #include <stdbool.h> void main() { bool var = true; if(var == 1) { printf("TRUE"); } else { printf("FALSE"); } }
Thus, this statement validates the statement that the default integer value of bool value true is 1 and for false, it’s 0.
Output:
TRUE
As seen in the above section, integer values can be implicitly interpreted to boolean values and used for further comparison as shown below–
#include <stdio.h> #include <stdbool.h> void main() { bool var1 = 25; bool var2 = 0; if(var1 == true) { printf("TRUE\n"); } else { printf("FALSE"); } if(var2 == true) { printf("TRUE"); } else { printf("FALSE"); } }
Now, we implicitly compared boolean value TRUE to the var1 and FALSE to var2 instead of comparing the actual numbers assigned
Output:
TRUE FALSE
Now, we have implemented logical AND operation on the boolean type values.
#include <stdio.h> #include <stdbool.h> void main() { bool bool_and = 0; bool var1 = true; bool var2 = false; bool_and = var1&&var2; printf("The logical AND value of var1 and var2: %d", bool_and); }
Output:
The logical AND value of var1 and var2: 0
Conclusion
By this, we have come to the end of this topic. Feel free to comment below in case you come across any question.
For more such posts related to C programming, do visit C programming with JournalDev.
Till then, Happy Learning!!