1. Overview of Operators
Operators are symbols used in the C language to perform various operations, and they are the basic elements that make up expressions. C provides a rich variety of operator types that can perform arithmetic operations, relational comparisons, logical evaluations, bit manipulations, and more. Mastering the use of operators is an important foundation for learning C programming.
2. Classification of Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations:
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (Remainder) | a % b |
| ++ | Increment | a++ or ++a |
| — | Decrement | a– or –a |
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b); // 13
printf("a - b = %d\n", a - b); // 7
printf("a * b = %d\n", a * b); // 30
printf("a / b = %d\n", a / b); // 3 (integer division)
printf("a %% b = %d\n", a % b); // 1
int c = 5;
printf("c++ = %d\n", c++); // Use c's value 5, then increment
printf("++c = %d\n", ++c); // Increment to 7, then use
return 0;
}
Output:

int a = 5, b = 2; float result = a / b; // Note: integer division, result is 2.0 not 2.5
float f = 10.5; int mod = f % 3; // Error: % operator cannot be used with floating-point numbers, only with integers
int i = 5; printf("%d", i++ + ++i); // Undefined behavior, results may vary across compilers
Notes:
-
Avoid using increment/decrement on the same variable multiple times in the same expression
-
Clearly distinguish between prefix (++i) and postfix (i++) forms
2. Relational Operators
Relational operators are used to compare the size relationship between two values, returning 1 (true) or 0 (false):
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("x == y: %d\n", x == y); // 0
printf("x != y: %d\n", x != y); // 1
printf("x > y: %d\n", x > y); // 0
printf("x < y: %d\n", x < y); // 1
return 0;
}
Running result:
Notes:
-
Use == for equality comparison, not =
3. Logical Operators
Logical operators are used to combine multiple conditions:
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a && b |
| || | Logical OR | a || b |
| ! | Logical NOT | !a |
#include <stdio.h>
int main() {
int a = 5; // 0101
int b = 3; // 0011
// Logical operators
printf("Logical AND: %d && %d = %d\n", a, b, a && b); // 1
printf("Logical OR: %d || %d = %d\n", a, b, a || b); // 1
printf("Logical NOT: !a = %d !b = %d\n", !a,!b); // 0 0
return 0;
}
Output result:
Notes:
-
&& is used for boolean logic evaluation
-
& is used for bitwise operations
4. Assignment Operators
Assignment operators are used to assign values to variables:
| Operator | Description | Equivalent to |
|---|---|---|
| = | Simple assignment | a = b |
| += | Add and assign | a += b → a = a + b |
| -= | Subtract and assign | a -= b → a = a – b |
| *= | Multiply and assign | a *= b → a = a * b |
| /= | Divide and assign | a /= b → a = a / b |
| %= | Modulus and assign | a %= b → a = a % b |
#include <stdio.h>
int main() {
int a = 10;
a += 5; // a = 15
a -= 3; // a = 12
a *= 2; // a = 24
a /= 4; // a = 6
a %= 5; // a = 1
printf("Final value of a: %d\n", a);
return 0;
}
Output result:
5. Bitwise Operators (Advanced)
Bitwise operators directly manipulate the binary bits of data:
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise NOT | ~a |
| << | Left shift | a << n |
| >> | Right shift | a >> n |
#include <stdio.h>
int main() {
unsigned char a = 5; // Binary representation: 00000101
unsigned char b = 3; // Binary representation: 00000011
printf("a & b = %d\n", a & b); // 00000001 → 1
printf("a | b = %d\n", a | b); // 00000111 → 7
printf("a ^ b = %d\n", a ^ b); // 00000110 → 6
printf("~a = %d\n", ~a & 0xFF); // 11111010 → 250
printf("a << 1 = %d\n", a << 1); // 00001010 → 10
printf("a >> 1 = %d\n", a >> 1); // 00000010 → 2
return 0;
}
Output result:
3. Operator Precedence
When an expression contains multiple operators, C determines the order of operations based on operator precedence and associativity:
| Precedence | Operator | Precedence | Operator | Precedence | Operator |
|---|---|---|---|---|---|
| 1 | <span>() Parentheses</span> |
6 | ==, != | 11 | || |
| 2 | *, /, % | 7 | & | 12 | <span>?:</span> (Ternary operator) |
| 3 | +, – | 8 | ^ | 13 | |
| 4 | <<, >> | 9 | | | 14 | |
| 5 | <, <=, >, >= | 10 | && | 15 |
4. Special Operators
1. Conditional Operator (Ternary Operator)
Syntax:
<span><span>condition ? expression1 : expression2</span></span>
int a = 10, b = 20; int max = (a > b) ? a : b; // max = 20
2. sizeof Operator
Returns the size (in bytes) of a variable or type:
printf("Size of int: %zu\n", sizeof(int)); // Usually 4
printf("Size of a: %zu\n", sizeof(a)); // Depends on the type of a
Notes:
-
The multiplication (*) operator has higher precedence than addition (+)
-
Use parentheses to clarify precedence when uncertain