“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“C Language Beginner’s Essential Knowledge Notes Series – 100 Articles”
20. Logical Operators: AND, OR, NOT (&&, ||, !), very simple, but there are two mistakes that most beginners will 100% make!
1. Basic Concepts of Logical Operators
The C language provides three logical operators for combining or negating conditional expressions:
| Operator | Name | Example | Description |
| && | Logical AND | a && b | True if both a and b are true |
| || | Logical OR | a || b | True if at least one of a or b is true |
| ! | Logical NOT | !a | Negates the truth value of a |
2. Detailed Truth Tables
1. Logical AND (&&)
| a | b | a && b |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
2. Logical OR (||)
| a | b | a || b |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
3. Logical NOT (!)
| a | !a |
| 0 | 1 |
| 1 | 0 |
3. Basic Usage Examples
1. Conditional Combination
int age = 25, score = 85;
if (age >= 18 && score >= 60) {
printf("Pass\n");
}
2. Range Check
int x = 15;
if (x < 0 || x > 100) {
printf("Out of range\n");
}
3. State Negation
int isReady = 0;
if (!isReady) {
printf("System not ready\n");
}
4. Short-Circuit Evaluation Characteristics
1. Short-Circuit of Logical AND
int a = 0, b = 1;
if (a && b++) { // a is false, b++ will not execute
// Will not execute
}
printf("%d\n", b); // Outputs 1 (not incremented)
2. Short-Circuit of Logical OR
int x = 1, y = 0;
if (x || y++) { // x is true, y++ will not execute
// Will execute
}
printf("%d\n", y); // Outputs 0 (not incremented)
5. Operator Precedence
- 1. Logical NOT (!) has the highest precedence
- 2. Arithmetic operators > Relational operators > Logical AND > Logical OR
- 3. Use parentheses to clarify precedence
Example:
if ((a > b) && (c < d)) // Clarifies precedence
6. Common Mistakes
1. Misusing Bitwise Operators
if (a & b) { // Bitwise AND, not logical check
// ...
}
Correct approach:
if (a && b) { // Logical AND
// ...
}
2. Confusing = and ==
if (a = 0) { // Assignment operation, always false
// ...
}
Correct approach:
if (a == 0) { // Comparison operation
// ...
}
Some students have contacted me, wanting to have a study exchange group. Previously, I was concerned about advertisements, so I didn’t create a group. Considering that having a group is indeed convenient, I will try to create one this time.
If you need it, hurry up and join, valid for 7 days.

———- End ———-
[Special Statement: This article is original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to use, opinions are for learning reference only~~]


“If you like C, please like it”
Click the bottom right corner to see more
“