What is Boolean in C?
Boolean is a data type in C that represents logical values. It can have two possible values: true or false. In C, true is represented by the integer value 1 and false is represented by the integer value 0.
Boolean is used in C to perform logical operations and to make decisions based on the result of these operations. For example, if we want to check if a certain condition is true or false, we can use a Boolean variable to store the result of this check and then use it in a conditional statement to execute different blocks of code based on the value of the Boolean variable.
Syntax:
bool variable_name;
This declares a Boolean variable with the name variable_name
.
Example:
bool is_sunny = true;
In above example, we declare a Boolean variable called is_sunny
and initialize it with the value true
.
Different ways to write Boolean in C:
1. Using the bool
keyword:
bool is_sunny = true;
2. Using the int
data type with the values 0 and 1:
int is_sunny = 1;
3. Using the char
data type with the values ‘0’ and ‘1’:
char is_sunny = '1';
All these methods are equivalent and can be used interchangeably. The choice of method depends on personal preference and coding style.
Examples of Boolean:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool is_sunny = true;
int temperature = 25;
if (is_sunny && temperature > 20) {
printf("It's a nice day.\n");
} else {
printf("It's not a nice day.\n");
}
return 0;
}
Output:
It's a nice day.
Explanation:
In above example, we declare a Boolean variable called is_sunny
and initialize it with the value true
. We also declare an integer variable called temperature
and initialize it with the value 25
. We then use the if
statement to check if is_sunny
is true and if the temperature
is greater than 20
. Since both conditions are true, the program prints “It’s a nice day.” to the console.
Boolean using Array and Typedef:
Boolean can also be used as an array or as a typedef. When used as an array, Boolean can store multiple true/false values. When used as a typedef, Boolean can be given a custom name, which can make the code more readable.
1. Example of Boolean using Array:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool weekdays[7] = {true, true, true, true, true, false, false};
for (int i = 0; i < 7; i++) {
if (weekdays[i]) {
printf("Day %d is a weekday.\n", i + 1);
} else {
printf("Day %d is a weekend.\n", i + 1);
}
}
return 0;
}
Output:
Day 1 is a weekday.
Day 2 is a weekday.
Day 3 is a weekday.
Day 4 is a weekday.
Day 5 is a weekday.
Day 6 is a weekend.
Day 7 is a weekend.
Explanation:
In above example, we declare an array called weekdays
that contains Boolean values. Each element in the array represents a day of the week, and is true
if it’s a weekday and false
if it’s a weekend. We then use a for
loop to iterate over each element in the array, and use an if
statement to check whether the current day is a weekday or a weekend. Finally, we print a message to the console indicating whether the current day is a weekday or a weekend.
2. Example of Boolean using Typedef:
#include <stdio.h>
#include <stdbool.h>
typedef bool Status;
int main() {
Status is_sunny = true;
if (is_sunny) {
printf("It's sunny today.\n");
} else {
printf("It's not sunny today.\n");
}
return 0;
}
Output:
It's sunny today.
In above example, we use the typedef
keyword to create a new data type called Status
that is equivalent to bool
. We then declare a variable called is_sunny
of type Status
, which can only have the values true
or false
. We use an if
statement to check whether is_sunny
is true
or false
, and print a message to the console accordingly.
Boolean using Logical Operators:
Boolean can also be used with logical operators such as &&
(AND), ||
(OR), and !
(NOT). These operators allow us to combine multiple Boolean expressions and perform logical operations on them.
1. Boolean using && (AND) Logical Operator:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isSunny = true;
bool isWarm = true;
if (isSunny && isWarm) {
printf("It's a great day for a picnic!\n");
} else {
printf("Sorry, the weather is not suitable for a picnic.\n");
}
return 0;
}
Output:
It's a great day for a picnic!
Explanation:
In above example, the &&
operator requires both isSunny
and isWarm
to be true
for the overall expression to be true
. Since both variables are true
, the message “It’s a great day for a picnic!” is printed to the console.
2. Boolean using || (OR) Logical Operator:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isRaining = true;
bool isSnowing = false;
if (isRaining || isSnowing) {
printf("Be sure to bring your umbrella.\n");
} else {
printf("No need for an umbrella today.\n");
}
return 0;
}
Output:
Be sure to bring your umbrella.
Explanation:
In this example, the ||
operator requires either isRaining
or isSnowing
to be true
for the overall expression to be true
. Since isRaining
is true
, the message “Be sure to bring your umbrella.” is printed to the console.
3. Boolean using ! (NOT) Logical Operator:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isRaining = true;
if (!isRaining) {
printf("No need for an umbrella today.\n");
} else {
printf("Be sure to bring your umbrella.\n");
}
return 0;
}
Output:
Be sure to bring your umbrella.
Explanation:
In this example, the !
operator negates the value of isRaining
. Since isRaining
is true
, the negated value is false
. Therefore, the message “Be sure to bring your umbrella.” is printed to the console.
Rules to follow while using Boolean in C:
- Boolean variables should be declared using the
bool
keyword or a compatible data type such asint
orchar
. - Boolean variables should be initialized with either the value
true
orfalse
. - When comparing Boolean variables, use logical operators such as
&&
and||
instead of arithmetic operators such as+
and-
. - Boolean variables should be used in conditional statements such as
if
andwhile
to make decisions based on the result of a logical operation.