Logical Operators in C
Logical operators combine multiple conditions. In C, any non-zero value is true, and 0 is false.
| Operator | Name | Meaning | Example |
|---|---|---|---|
| && | AND | BOTH must be true | age>18 && salary>25000 |
| || | OR | At LEAST ONE must be true | hasID || hasTicket |
| ! | NOT | Reverses true/false | !isEmpty (not empty) |
Truth Tables
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 1 (True) | 1 (True) | 1 (True) | 1 (True) | 0 (False) |
| 1 (True) | 0 (False) | 0 (False) | 1 (True) | 0 (False) |
| 0 (False) | 1 (True) | 0 (False) | 1 (True) | 1 (True) |
| 0 (False) | 0 (False) | 0 (False) | 0 (False) | 1 (True) |
c
#include <stdio.h>
int main() {
int age = 25, salary = 35000;
int hasID = 1, hasTicket = 0;
// AND — both conditions must be true
if (age >= 18 && salary >= 25000)
printf("Eligible for bank loan\n");
// OR — at least one must be true
if (hasID || hasTicket)
printf("Can enter the venue\n");
// NOT — reverses the condition
if (!hasTicket)
printf("Please buy a ticket first\n");
// Combined — real world: student passes if marks>=40 AND attendance>=75
int marks = 85, attendance = 80;
if (marks >= 40 && attendance >= 75)
printf("Student can appear in exam\n");
else if (marks < 40 && attendance < 75)
printf("Fail on BOTH counts — cannot appear\n");
else
printf("One condition met — needs approval\n");
return 0;
}▶ Output
Eligible for bank loan Can enter the venue Please buy a ticket first Student can appear in exam
💡 Tip: Short-circuit evaluation: In
A && B, if A is false, B is never evaluated. In A || B, if A is true, B is never evaluated. This makes code more efficient.