Table of Contents
The logical OR operator || combines two conditions. The result is 1 when at least one operand is non-zero. It produces 0 only when both operands are zero.
#include <stdio.h>
int main()
{
int age = 16;
int has_permission = 1;
printf("Result: %d\n", age >= 18 || has_permission);
return 0;
}
Example Output #
Result: 1
Explanation #
The expression:
age >= 18 || has_permission
contains two operands.
age >= 18 produces 0 because age is 16. has_permission contains the non-zero value 1.
Since at least one operand is non-zero, the || expression produces 1.
The logical OR operator follows these results:
| Operand 1 | Operand 2 | Result |
|---|---|---|
0 |
0 |
0 |
0 |
non-zero | 1 |
| non-zero | 0 |
1 |
| non-zero | non-zero | 1 |
C uses short-circuit evaluation for ||. If the left operand evaluates to a non-zero value, the right operand is not evaluated because the overall result is already known to be true.