1. Introduction
The precedence of operators in C language is crucial for correctly understanding program behavior and achieving the intended results. Misinterpreting the order of evaluation can easily lead to unexpected results or bugs. In this article, we will explain in detail the precedence and associativity rules of major C operators, with concrete examples to help deepen your understanding.
2. Types of Operators and Basic Usage
2.1 Arithmetic Operators
Arithmetic operators are the basic tools for performing calculations with numbers. These operators are frequently used in programs, so it is essential to understand the basics well.
+
(Addition): Adds two numbers.-
(Subtraction): Subtracts one number from another.*
(Multiplication): Multiplies two numbers./
(Division): Divides one number by another (when dividing integers, the result is truncated).%
(Modulus): Returns the remainder of a division operation.
Example:
int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int prod = a * b; // 30
int quot = a / b; // 3
int rem = a % b; // 1
2.2 Comparison Operators
Comparison operators compare two values and return either true (1) or false (0). They are often used in conditional statements and loops.
>
(Greater than): True if the left value is greater than the right.<
(Less than): True if the left value is less than the right.>=
(Greater than or equal to): True if the left value is greater than or equal to the right.<=
(Less than or equal to): True if the left value is less than or equal to the right.==
(Equal to): True if both values are equal.!=
(Not equal to): True if the values are different.
int a = 5, b = 10;
if (a < b) {
printf("a is less than b\n"); // This will be printed
}

3. Operator Precedence and Associativity
3.1 Operator Precedence
Operator precedence determines which operations are performed first in expressions containing multiple operators. Here is a partial list of operator precedence in C, ordered from highest to lowest.
Precedence | Operator | Description |
---|---|---|
1 | () [] -> . | Function call, array access, pointer member |
2 | ++ -- | Postfix increment/decrement |
3 | ++ -- | Prefix increment/decrement, unary operators |
4 | * / % | Multiplication, division, modulus |
5 | + - | Addition, subtraction |
6 | << >> | Bitwise shift |
7 | < <= > >= | Relational operators |
8 | == != | Equality, inequality |
9 | & | Bitwise AND |
10 | ^ | Bitwise XOR |
11 | | | Bitwise OR |
12 | && | Logical AND |
13 | || | Logical OR |
14 | ? : | Ternary (conditional) operator |
15 | = += -= | Assignment, compound assignment |
16 | , | Comma operator |
3.2 Associativity
Associativity determines the direction of evaluation when multiple operators of the same precedence appear in an expression. Most operators are left-associative, but some (such as assignment and conditional operators) are right-associative.
- Left-associative: Operators like
*
,+
,-
are evaluated from left to right. - Right-associative: Assignment and conditional operators
? :
are evaluated from right to left.
int a = 5, b = 10, c = 15;
int result = a - b + c; // Left-associative: (a - b) + c = 0
4. Cautions Regarding the Precedence of Specific Operators
4.1 Logical Operators
Logical AND (&&
) and logical OR (||
) are used to combine conditions, but &&
has higher precedence than ||
. This can lead to confusion in code like the following:
int a = 1, b = 0, c = 1;
if (a && b || c) {
printf("True\n"); // This will be printed
}
In this example, a && b
is evaluated first, and then the result is combined with c
using ||
. To clarify the intended order, use parentheses explicitly:
if ((a && b) || c) {
// More explicit evaluation
}
4.2 Bitwise Operators
Bitwise operators (&
, |
, ^
) operate at the bit level, but have lower precedence than arithmetic or comparison operators, so be careful in complex expressions.
int x = 5; // 0101
int y = 3; // 0011
int result = x & y; // 0001 (bitwise AND)
5. Example Programs
Here is an example program that often causes confusion regarding operator precedence. The evaluation order of ||
and &&
can easily be misunderstood.
#include <stdio.h>
int main() {
int a = 0, b = 1;
if (a == 0 || a == 1 && b == 0) {
printf("True\n"); // This will be printed
}
return 0;
}
In this example, a == 1 && b == 0
is evaluated first, which may lead to unintended results. To get the correct result, clarify the evaluation order using parentheses.
if ((a == 0 || a == 1) && b == 0) {
printf("True\n");
}
6. Conclusion
Understanding operator precedence in C is essential for ensuring correct program behavior. When handling complex expressions, be sure to understand precedence and associativity, and use parentheses as needed to clarify the order of operations. By paying close attention to operator precedence, you can write safer and more efficient code.