Summary: in this tutorial, you’ll learn about the C logical operators, including the logical AND, OR, and NOT operators.
Introduction to the C logical operators #
C supports three logical operators including the logical AND operator, logical OR operator, and logical NOT operator as shown in the following table:
Operator | Meaning |
---|---|
&& | logical AND |
|| | logical OR |
! | logical NOT |
The logical AND and OR operators take two operands, while the logical NOT operator takes one.
The logical operators evaluate each operand to a truth value, either true (1) or false (0). The result of a logical operator is either 0 or 1.
Note that C doesn’t natively support the Boolean type. It uses the int type instead. So 0 means false, and other values mean true.
The logical AND and OR operators evaluate the operand from left to right. If the value of the first operand is sufficient to decide the result of the operation, it won’t evaluate the second operand. In other words, the logical AND and OR operators short-circuit.
The logical AND operator (&&) #
The logical AND operator takes two operands and returns true (1) if both operands are evaluated as true (1). Otherwise, it returns false (0).
The following table illustrates the result of the logical AND operator:
a | b | a && b |
---|---|---|
true | true | true |
true | false | false |
false | false | false |
false | true | false |
If the first operand evaluates to true (1), the logical AND operator immediately returns true (1) and skip evaluating the second operand.
The following program uses the logical AND operator to check if the age is greater than 16 and less than 70:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int age = 22;
bool can_drive = age > 16 && age < 70;
printf("%d\n", can_drive); // 1 or true
return 0;
}
Code language: C++ (cpp)
Output:
1
Code language: C++ (cpp)
The logical OR operator #
The logical OR operator takes two operands and returns true (1) if both operands evaluate to true (1) or false (0) otherwise.
The following table illustrates the result of the logical OR operator:
a | b | a || b |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
The logical OR operator will stop evaluating the second operand if the first operand evaluates to true.
The following example uses the logical OR operator to check if a person can get a free ticket based on his/her age:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int age = 85;
bool free_ticket = age < 5 || age > 70;
printf("%d\n", free_ticket); // 1 or true
return 0;
}
Code language: C++ (cpp)
In this example, the logical OR operator first evaluates the first operand:
age < 5
Code language: C++ (cpp)
It returns false. Therefore, the logical OR operator evaluates the second operand:
age > 70
Code language: C++ (cpp)
This returns true. Therefore, the whole expression returns true:
age < 5 || age > 70
Code language: C++ (cpp)
The logical NOT operator (!) #
The logical NOT operator takes one operand and reverses that operand:
a | !a |
---|---|
true | false |
false | true |
The following example uses the logical NOT operator to reverse the value of a Boolean variable:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool is_active = true;
printf("%d\n", is_active);
printf("%d\n", !is_active);
return 0;
}
Code language: C++ (cpp)
Summary #
- C supports the logical AND operator (&&), the logical OR operator (||), and the logical NOT operator (!).
- The logical AND operator (&&) returns true if both operands are true or false otherwise.
- The logical OR operator (||) returns false if both operands are false or true otherwise.
- The logical NOT operator (!) negates the value of an operand.