C Notes
Learn logical operators in C: AND (&&), OR (||), and NOT (!). Understand short-circuit evaluation, truth tables, operator precedence, and practical examples for decision-making.
Logical operators allow you to combine multiple conditions into a single expression. When a simple "is x greater than 5?" isn't enough and you need "is x greater than 5 AND less than 100?", logical operators are what you reach for. They're essential for building complex decision-making logic in any real-world C program.
The Three Logical Operators
C provides three logical operators:
| Operator | Name | Description | Example | ||||
|---|---|---|---|---|---|---|---|
&& | Logical AND | True if BOTH operands are true | (a > 0 && b > 0) | ||||
| ` | ` | Logical OR | True if EITHER operand is true | `(a > 0 | b > 0)` | ||
! | Logical NOT | Inverts the truth value | !(a > 0) |
Just like relational operators, logical operators return 1 for true and 0 for false.
Logical AND (&&)
The AND operator returns true only when both conditions are true. If either condition is false, the entire expression is false.
Truth Table for AND:
| A | B | A && B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
#include <stdio.h>
int main() {
int age = 25;
float salary = 55000;
// Both conditions must be true
if (age >= 21 && salary >= 50000) {
printf("Eligible for premium credit card\n");
}
// Range checking
int score = 85;
if (score >= 80 && score <= 90) {
printf("Score %d falls in B+ range\n", score);
}
// Multiple AND conditions
int x = 15;
if (x > 0 && x < 100 && x % 5 == 0) {
printf("%d is positive, less than 100, and divisible by 5\n", x);
}
return 0;
}Eligible for premium credit card Score 85 falls in B+ range 15 is positive, less than 100, and divisible by 5
Logical OR (||)
The OR operator returns true when at least one condition is true. It returns false only when both conditions are false.
Truth Table for OR:
| A | B | A \ | \ | B |
|---|---|---|---|---|
| 0 | 0 | 0 | ||
| 0 | 1 | 1 | ||
| 1 | 0 | 1 | ||
| 1 | 1 | 1 |
Excellent performance! It's the weekend! 'e' is a vowel
Logical NOT (!)
The NOT operator is a unary operator that inverts the truth value of its operand. If the expression is true (non-zero), ! makes it false (0), and vice versa.
#include <stdio.h>
int main() {
int loggedIn = 0; // 0 = false, not logged in
if (!loggedIn) {
printf("Please log in first\n");
}
// Inverting conditions
int age = 15;
if (!(age >= 18)) {
printf("Not an adult (age: %d)\n", age);
}
// NOT with values
printf("!0 = %d\n", !0);
printf("!1 = %d\n", !1);
printf("!5 = %d\n", !5); // Any non-zero becomes 0
printf("!(-3) = %d\n", !(-3)); // Negative non-zero also becomes 0
return 0;
}Please log in first Not an adult (age: 15) !0 = 1 !1 = 0 !5 = 0 !(-3) = 0
Short-Circuit Evaluation
This is one of the most important concepts related to logical operators in C. Short-circuit evaluation means that C stops evaluating a logical expression as soon as the result is determined:
- For
&&: If the left operand is false, the right operand is NOT evaluated (because false AND anything = false) - For
||: If the left operand is true, the right operand is NOT evaluated (because true OR anything = true)
Short-circuit prevented division by zero! x = 10 (not incremented due to short-circuit)
Demonstrating Side Effects with Short-Circuit
AND: b was incremented to 3 OR: c was NOT incremented, c = 3
Warning: Never rely on side effects (like ++) in short-circuited expressions. It makes code hard to read and debug.
Combining Logical Operators
You can build complex conditions by combining AND, OR, and NOT:
You are eligible to drive Access granted
Operator Precedence
The precedence of logical operators relative to other operators:
| Priority | Operator | Description | ||
|---|---|---|---|---|
| Higher | ! | Logical NOT | ||
| ... | Arithmetic, Relational | (evaluated first) | ||
&& | Logical AND | |||
| Lower | ` | ` | Logical OR |
This means && has higher precedence than ||:
a || b && c = 1 (a || b) && c = 1 0 || 0 && 1 = 0 (0 || 0) && 1 = 0
Practical Example: Leap Year Check
A year is a leap year if it's divisible by 4, except for century years which must be divisible by 400:
Enter a year: 2024 2024 is a leap year
Practical Example: Triangle Validity
Sides 3, 4, 5 form a valid triangle It's a scalene triangle
Difference Between Logical and Bitwise Operators
Don't confuse logical operators with bitwise operators:
| Logical | Bitwise | Difference | ||||||
|---|---|---|---|---|---|---|---|---|
&& | & | && short-circuits; & evaluates both and works bit-by-bit | ||||||
| ` | ` | ` | ` | ` | short-circuits; | ` evaluates both and works bit-by-bit | ||
! | ~ | ! returns 0 or 1; ~ flips all bits |
Logical: 2 && 3 = 1 Bitwise: 2 & 3 = 2 Logical: 2 || 3 = 1 Bitwise: 2 | 3 = 3
Interview Questions
Q1: What is short-circuit evaluation in C? Give an example where it prevents a crash.
Short-circuit evaluation means C stops evaluating a logical expression once the result is determined. For &&, if the left side is false, the right side is skipped. Example: if (ptr != NULL && ptr->value > 0) — if ptr is NULL, the dereference ptr->value is never executed, preventing a segmentation fault.
Q2: What is the output of printf("%d", !0 + !0 + !0)?
The output is 3. !0 evaluates to 1 (NOT of false is true). So the expression becomes 1 + 1 + 1 = 3.
Q3: Is && the same as & in C?
No. && is the logical AND operator — it evaluates truthiness and returns 0 or 1 with short-circuit behavior. & is the bitwise AND operator — it performs bit-by-bit AND on the binary representation and evaluates both operands regardless.
Q4: What is the precedence order of !, &&, and ||?
! has the highest precedence (it's a unary operator), followed by &&, and then || has the lowest precedence among the three. So !a && b || c is evaluated as ((!a) && b) || c.
Q5: Can you use integers other than 0 and 1 with logical operators?
Yes. In C, any non-zero value is considered true, and zero is false. So 5 && 3 evaluates to 1, and 0 || -7 evaluates to 1. The NOT operator !5 gives 0, and !0 gives 1.
Summary
- C has three logical operators:
&&(AND),||(OR), and!(NOT) - They always return
1(true) or0(false) as integers - Short-circuit evaluation is a key feature — the second operand may not be evaluated
&&has higher precedence than||, so use parentheses for clarity- Don't confuse logical operators (
&&,||) with bitwise operators (&,|) - Any non-zero value is treated as true in logical expressions
- Short-circuit evaluation can be used to guard against null pointers and division by zero
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Logical Operators in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, operators, logical, logical operators in c programming
Related C Programming Topics