C OR Logical Operator
C OR Logical Operator is used to compute logical OR operation between two boolean values. The operands to OR Operator can be boolean variables, or conditions that return a boolean value, or integers equivalent to the boolean values.
C OR Operator takes two boolean values as operands and returns an int value equivalent to the resulting boolean value .
operand_1 || operand_2
Truth Table
The truth table of OR Logical Operator is
| Operand 1 | Operand 2 | Returns |
| true | true | 1 |
| true | false | 1 |
| false | true | 1 |
| false | false | 0 |
C OR returns true if at-least one of the operand is true.
Integer value of 1 is equivalent to true, and 0 is equivalent to false.
Examples
The following example demonstrates the usage of OR logical operator with different boolean values.
main.c
#include <stdio.h>
#include <stdbool.h>
int main() {
bool x, y, result;
x = true;
y = true;
result = x || y;
printf("%d || %d : %d\n", x, y, result);
x = true;
y = false;
result = x || y;
printf("%d || %d : %d\n", x, y, result);
x = false;
y = true;
result = x || y;
printf("%d || %d : %d\n", x, y, result);
x = false;
y = false;
result = x || y;
printf("%d || %d : %d\n", x, y, result);
}
Output
1 || 1 : 1
1 || 0 : 1
0 || 1 : 1
0 || 0 : 0
Program ended with exit code: 0
The following example demonstrates the usage of OR logical operator in combining boolean conditions and forming a compound condition.
main.c
#include <stdio.h>
int main() {
int a = 7;
if ((a < 10) || (a % 2 == 0)) {
printf("%d is even, or less than 10.\n", a);
}
}
Output
7 is even, or less than 10.
Program ended with exit code: 0
In the above example, a < 10 is a condition that checks if a is less than 10 and a % 2 == 0 is another condition that checks if a is even number.
If we would like to check if either of the conditions a < 10 or a % 2 == 0 is true, we use OR Logical Operator.
Conclusion
In this C Tutorial, we learned what C OR Logical Operator is, and how to use it with conditional expressions.
