C OR Logical Operator
C AND Logical Operator is used to compute logical AND operation between two boolean values. The operands given to AND Operator can be boolean variables, or conditions that return a boolean value, or integers equivalent to the boolean values.
C AND 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 AND Logical Operator is
Operand 1 | Operand 2 | Returns |
true | true | 1 |
true | false | 0 |
false | true | 0 |
false | false | 0 |
C OR returns true if both the operands are true.
Integer value of 1
is equivalent to true
, and 0
is equivalent to false
.
Examples
The following example demonstrates the usage of AND 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 : 0
0 && 1 : 0
0 && 0 : 0
Program ended with exit code: 0
The following example demonstrates the usage of AND logical operator in combining boolean conditions and forming a compound condition.
main.c
#include <stdio.h>
int main() {
int a = 8;
if ((a < 10) && (a % 2 == 0)) {
printf("%d is even, and less than 10.\n", a);
}
}
Output
8 is even, and 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 both the conditions a < 10
or a % 2 == 0
are true, we use AND Logical Operator.
Conclusion
In this C Tutorial, we learned what C AND Logical Operator is, and how to use it with conditional expressions.