Logical operators in C are used to combine multiple conditions and evaluate them as a single expression. They are commonly used in decision-making statements such as if, if-else, while, and for loops to control program flow.
- Logical operators return either 1 (true) or 0 (false) based on the evaluation of the expression.
- They help in combining, negating, or testing multiple conditions efficiently.
Types of Logical Operators in C
C provides three logical operators:
1. Logical AND Operator ( && )
The logical AND operator (&&) returns true only if both operands are non-zero. Otherwise, it returns false (0). The return type of the result is int. Below is the truth table for the logical AND operator.
X | Y | X && Y |
|---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Syntax
(operand_1 && operand_2)
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (a > 0 && b > 0)
{
printf("Both values are greater than 0\n");
}
else
{
printf("One or both values are less than or equal to 0\n");
}
return 0;
}
Output
Both values are greater than 0
2. Logical OR Operator ( || )
The logical OR operator returns true if any one of the operands is non-zero. Otherwise, it returns false i.e., 0 as the value. Below is the truth table for the logical OR operator.
| X | Y | X || Y |
|---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Syntax
(operand_1 || operand_2)
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0)
{
printf("Any one of the given value is "
"greater than 0\n");
}
else
{
printf("Both values are less than or equal to 0\n");
}
return 0;
}
Output
Any one of the given value is greater than 0
3. Logical NOT Operator ( ! )
The logical NOT operator (!) reverses the result of a condition. If the condition is true, it becomes false, and if the condition is false, it becomes true. Below is the truth table for the logical NOT operator.
| X | !X |
|---|---|
0 | 1 |
1 | 0 |
Syntax
!(operand_1 && operand_2)#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (!(a > 0 && b > 0))
{
// condition returned true but logical NOT operator changed it to false
printf("Both values are greater than 0\n");
}
else
{
printf("Both values are less than 0\n");
}
return 0;
}
Short Circuit Logical Operators
When the result can be determined by evaluating the preceding Logical expression without evaluating the other operands, it is known as short-circuiting.
Short-circuiting can be seen in the equation having more than one Logical operator. They can either AND, OR, or both.
1. Short Circuiting in Logical AND Operator
The logical AND operator returns true only if both operands evaluate to true. If the first operand is false, the second operand is not evaluated because the overall result will always be false. This is because even if the further operands evaluate to true, the entire condition will still return false.
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 0;
// c is 0(false), so (a / b) is not evaluated and division will not be done
if (c && (a = a / b))
printf("This won't be printed\n");
printf("%d", a);
return 0;
}
Output
10
2. Short Circuiting in Logical OR Operator
OR operator returns true if at least one operand evaluates to true. If the first operand is true, the second operand is not evaluated because the overall result is already true. This is because even if the further operands evaluate to false, the entire condition will still return true.
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 1;
// c is 1(true), so (a / b) is not evaluated and division will not be done
if (c || (a = a / b))
printf("This will be printed\n");
printf("%d", a);
return 0;
}
Output
This will be printed 10