Introduction
In the previous section, we learned about the four basic types of operators in C language: arithmetic, assignment, relational, and conditional operators.
Fundamentals of C Language (Part Three): Operators and Expressions (Part One) – A Logical Journey from Calculation to Judgment
These operators enable programs to perform “calculations and judgments”. But this is only half of the world of operators.
In this article, we will delve into deeper content:
- ⚙️ Logical Operators: Making judgments more flexible
- 🧮 Bitwise Operators: Mastering operations at the binary level
- 🔁 Increment and Decrement Operators (++ / –): Tools for looping and counting
- 🧩 Operator Precedence and Associativity: Understanding the true execution order of expressions
By mastering these, you will no longer just “write code”, but you will be able to “understand what the code is calculating”.
1. Logical Operators
Logical operators are commonly used in conditional judgments to connect multiple logical expressions. Their results can only be two: true (1) or false (0).
| Operator | Meaning | Example | Result |
|---|---|---|---|
<span>&&</span> |
Logical AND | <span>(a > 0) && (b> 0)</span> |
True only if both conditions are true |
| || | Logical OR | <span>(a > 0) || (b > 0)</span> |
True if at least one condition is true |
<span>!</span> |
Logical NOT | <span>!(a > 0)</span> |
Returns false if the condition is true, and true if it is false |
Example: Application of Logical Judgments
#include <stdio.h>
int main() {
int a = 5, b = -3;
printf("(a > 0) && (b > 0):%d\n", (a > 0) && (b > 0));
printf("(a > 0) || (b > 0):%d\n", (a > 0) || (b > 0));
printf("!(a > 0):%d\n", !(a > 0));
return 0;
}
Output:
(a > 0) && (b > 0):0
(a > 0) || (b > 0):1
!(a > 0):0
Tip:
&& and || operators have short-circuit characteristics:
- For &&, if the first expression is false, the second will not be evaluated;
- For ||, if the first expression is true, the second will not be evaluated.
2. Bitwise Operators
Bitwise operators are a very low-level but extremely powerful part of the C language. They directly operate on the binary bits of integers and are widely used in embedded development and performance optimization.
| Operator | Meaning | Example | Explanation of Result |
|---|---|---|---|
<span>&</span> |
Bitwise AND | <span>a & b</span> |
Result is 1 if both bits are 1 |
<span>|</span> |
Bitwise OR | <span>a | b</span> |
Result is 1 if at least one bit is 1 |
<span>^</span> |
Bitwise XOR | <span>a ^ b</span> |
Result is 1 if bits are different, 0 if they are the same |
<span>~</span> |
Bitwise NOT | <span>~a</span> |
0 becomes 1, 1 becomes 0 |
<span><<</span> |
Left Shift | <span>a << n</span> |
Shifts left by n bits (equivalent to multiplying by 2ⁿ) |
<span>>></span> |
Right Shift | <span>a >> n</span> |
Shifts right by n bits (equivalent to dividing by 2ⁿ) |
Example: Basic Bitwise Operations
#include <stdio.h>
int main() {
int a = 6; // Binary: 00000110
int b = 3; // Binary: 00000011
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
Output:
a & b = 2
a | b = 7
a ^ b = 5
~a = -7
a << 1 = 12
a >> 1 = 3
💡 Analysis:
- The binary representation of a = 6 is 00000110;
- The binary representation of b = 3 is 00000011;
- a & b retains only the bits where both are 1 → 00000010 → 2;
- a << 1 shifts left by one bit, equivalent to 6 * 2 = 12;
- ~a negates, and the sign bit also flips, resulting in a negative number.
3. Increment and Decrement Operators (++ / –)
These two operators are commonly used in loops or counting and are one of the most common shorthand forms.
| Operator | Meaning | Example | Equivalent Form |
|---|---|---|---|
<span>++a</span> |
Pre-increment | <span>++a</span> |
<span>a = a + 1</span>, increment first then use |
<span>a++</span> |
Post-increment | <span>a++</span> |
<span>a = a + 1</span>, use first then increment |
<span>--a</span> |
Pre-decrement | <span>--a</span> |
<span>a = a - 1</span>, decrement first then use |
<span>a--</span> |
Post-decrement | <span>a--</span> |
<span>a = a - 1</span>, use first then decrement |
Example: Difference Between Pre and Post
#include <stdio.h>
int main() {
int a = 5;
printf("a++ = %d\n", a++); // Output first, then increment
printf("a = %d\n", a);
printf("++a = %d\n", ++a); // Increment first, then output
printf("a = %d\n", a);
return 0;
}
Output:
a++ = 5
a = 6
++a = 7
a = 7
💡 Tip:
- Be particularly careful when mixing ++a and a++ in expressions;
- Especially in complex statements (e.g., a = a++ + ++a;), which may lead to unpredictable results.
4. Operator Precedence and Associativity
When an expression contains multiple operators, precedence determines the order of evaluation. If the precedence is the same, then associativity decides.
Common Operator Precedence Table (from high to low)
| Precedence | Operator | Associativity | Description |
|---|---|---|---|
| 1 | <span>()、</span><code><span>[]</span>、<span>.</span>、<span>-></span> |
Left to Right | Parentheses and member operations |
| 2 | <span>++、</span><code><span>--</span> (prefix) |
Right to Left | Increment and Decrement |
| 3 | <span>*、</span><code><span>/</span>、<span>%</span> |
Left to Right | Multiplication, Division, Modulus |
| 4 | <span>+、</span><code><span>-</span> |
Left to Right | Addition and Subtraction |
| 5 | <span><、</span><code><span><=</span>、<span>></span>、<span>>=</span> |
Left to Right | Relational Comparison |
| 6 | <span>==、</span><code><span>!=</span> |
Left to Right | Equality Comparison |
| 7 | <span>&&</span> |
Left to Right | Logical AND |
| 8 | <span>||</span> |
Left to Right | Logical OR |
| 9 | <span>?:</span> |
Right to Left | Conditional Operation |
| 10 | <span>=、</span><code><span>+=</span>、<span>-=</span> etc. |
Right to Left | Assignment |
| 11 | <span>, (comma)</span> |
Left to Right | Expression Separator |
Example: Understanding Precedence
#include <stdio.h>
int main() {
int a = 2, b = 3, c = 4;
int result = a + b * c; // Multiplication takes precedence
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 14
💡 Tip:
- The rule of multiplying before adding is consistent with mathematics;
- Using () to clarify precedence is the safest practice, for example: result = (a + b) * c;
Conclusion
After mastering this content, you can read and analyze almost all common C language expressions. They are the foundation of conditional judgments, loop structures, and algorithm implementations.
In the next article, we will enter the world of control statements (<span>if, switch, while, for</span>), truly beginning to “let the code make decisions”.