Logical Operators in C Programming
What are Logical Operators?
Logical operators combine multiple conditions and return a boolean-like result.
List of Logical Operators
&&— logical AND, true only if both conditions are true||— logical OR, true if at least one condition is true!— logical NOT, reverses the result
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both positive");
}
if (!(a == b)) {
printf("a is not equal to b");
}
Short-Circuit Evaluation
C evaluates logical expressions left to right and stops as soon as the result is determined — e.g. in &&, if the first condition is false the second is never evaluated.
Truth Table and What C Really Returns
C did not originally have a boolean type. Any non-zero value counts as true and zero counts as false. The logical operators always produce the int value 1 for true and 0 for false.
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | non-zero | 0 | 1 | 1 |
| non-zero | 0 | 0 | 1 | 0 |
| non-zero | non-zero | 1 | 1 | 0 |
So printf("%d", 7 && -3); prints 1, not 7 or -3. If you prefer readable code, include stdbool.h and use bool, true and false.
Precedence: Where Parentheses Save You
From highest to lowest, the relevant order is: !, then relational operators like < and >=, then equality == and !=, then &&, and finally ||. Because AND binds tighter than OR, this condition may not mean what you think:
/* Reads as: isAdmin || (isOwner && isActive) */
if (isAdmin || isOwner && isActive) { ... }
/* If you meant "admin or owner, and in any case active": */
if ((isAdmin || isOwner) && isActive) { ... }
Another trap is !x == 5. Since ! runs first, it becomes (!x) == 5, which is almost always false. Write !(x == 5) or simply x != 5.
Putting Short-Circuiting to Work
Short-circuit evaluation is not just a speed trick; it lets you guard risky operations. The right side runs only when the left side allows it.
#include <stdio.h>
#include <string.h>
int main(void) {
int count = 0, total = 50;
const char *name = NULL;
/* Safe: division happens only when count is non-zero */
if (count != 0 && total / count > 10) {
printf("High average\n");
}
/* Safe: strlen is never called on a NULL pointer */
if (name != NULL && strlen(name) > 0) {
printf("Hello, %s\n", name);
} else {
printf("No name given\n");
}
return 0;
}
Swap the two sides of either condition and the program could crash. Order matters: put the safety check first.
Mistakes and Handy Rules
- Using & instead of &&. The single ampersand is bitwise AND. It does not short-circuit, and
2 & 4is 0 even though both values are true. - Writing = instead of ==.
if (x = 0)assigns zero and is always false. Turn on compiler warnings with-Wallto catch it. - Chaining comparisons.
if (1 < x < 10)does not check a range. Writeif (x > 1 && x < 10). - Side effects on the right. In
a > 0 && i++, the increment may never happen. Keep conditions free of updates.
De Morgan's laws help you simplify negations: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. Use them to remove confusing double negatives from your conditions.
Ready to master real-world C Programming development?
Learn C Programming hands-on with mentor-led, live sessions.