C Logical Operators
In C, logical operators are used to perform logical operations on expressions. These operators evaluate Boolean values and are commonly used in decision-making constructs such as if
statements and loops.
The three logical operators in C are &&
(Logical AND), ||
(Logical OR), and !
(Logical NOT).
Logical Operators in C
The following table gives the operator symbol and description for each logical operator in C.
Operator Name | Operator Symbol | Description |
---|---|---|
Logical AND | && | Returns true if both operands are true; otherwise, returns false. |
Logical OR | || | Returns true if at least one operand is true; otherwise, returns false. |
Logical NOT | ! | Returns the opposite of the operand’s truth value. Converts true to false and vice versa. |
Examples of Logical Operators
1. Using Logical AND (&&
)
In this example, we will use the Logical AND operator to check whether two conditions are true.
main.c
#include <stdio.h>
int main() {
int age = 25;
int hasLicense = 1; // 1 means true
if (age >= 18 && hasLicense) {
printf("You are eligible to drive.\n");
} else {
printf("You are not eligible to drive.\n");
}
return 0;
}
Explanation:
- We declare an integer
age
and set it to 25. - We declare
hasLicense
with a value of 1 (true). - The
if
condition usesage >= 18 && hasLicense
to check if both conditions are true. - Since both conditions are true, “You are eligible to drive.” is printed.
Output:
You are eligible to drive.
2. Using Logical OR (||
)
In this example, we will use the Logical OR operator to check if at least one of the conditions is true.
main.c
#include <stdio.h>
int main() {
int raining = 1; // 1 means true
int umbrella = 0; // 0 means false
if (raining || umbrella) {
printf("You should stay dry.\n");
} else {
printf("You might get wet.\n");
}
return 0;
}
Explanation:
- We declare two integer variables
raining
(set to 1) andumbrella
(set to 0). - The
if
condition checks if eitherraining
orumbrella
is true. - Since
raining
is 1 (true), the condition evaluates to true, and “You should stay dry.” is printed.
Output:
You should stay dry.
3. Using Logical NOT (!
)
In this example, we will use the Logical NOT operator to reverse the Boolean value of a condition.
main.c
#include <stdio.h>
int main() {
int isDaytime = 0; // 0 means false
if (!isDaytime) {
printf("It is nighttime.\n");
} else {
printf("It is daytime.\n");
}
return 0;
}
Explanation:
- We declare an integer variable
isDaytime
and set it to 0 (false). - The
if
condition uses!isDaytime
to negate the value. - Since
isDaytime
is false,!isDaytime
becomes true. - The message “It is nighttime.” is printed.
Output:
It is nighttime.
Conclusion
In this tutorial, we explored the three logical operators in C:
- Logical AND (
&&
): Returns true if both conditions are true. - Logical OR (
||
): Returns true if at least one condition is true. - Logical NOT (
!
): Negates the Boolean value of a condition.
Logical operators are essential for controlling flow in C programs, making decisions, and handling multiple conditions efficiently.