Popular Tutorials
Start Learning CCertification Courses
Created with over a decade of experience and thousands of feedback.
C Precedence And Associativity Of Operators
In this tutorial, you'll learn about the precedence and associativity of operators with the help of examples.
Precedence of operators
The precedence of operators determines which operator is executed first if there is more than one operator in an expression.
Let us consider an example:
int x = 5 - 17* 6;
In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first. Then the expression involving - is evaluated as the precedence of - is higher than that of =.
Here's a table of operators precedence from higher to lower. The property of associativity will be discussed shortly.
Operators Precedence & Associativity Table
| Operator | Meaning of operator | Associativity |
|---|---|---|
| () [] -> . |
Functional call Array element reference Indirect member selection Direct member selection |
Left to right |
| ! ~ + - ++ -- & * sizeof (type) |
Logical negation Bitwise(1 's) complement Unary plus Unary minus Increment Decrement Dereference (Address) Pointer reference Returns the size of an object Typecast (conversion) |
Right to left |
| * / % |
Multiply Divide Remainder |
Left to right |
| + - |
Binary plus(Addition) Binary minus(subtraction) |
Left to right |
| << >> |
Left shift Right shift |
Left to right |
| < <= > >= |
Less than Less than or equal Greater than Greater than or equal |
Left to right |
| == != |
Equal to Not equal to |
Left to right |
| & | Bitwise AND | Left to right |
| ^ | Bitwise exclusive OR | Left to right |
| | | Bitwise OR | Left to right |
| && | Logical AND | Left to right |
| || | Logical OR | Left to right |
| ?: | Conditional Operator | Right to left |
| = *= /= %= += -= &= ^= |= <<= >>= |
Simple assignment Assign product Assign quotient Assign remainder Assign sum Assign difference Assign bitwise AND Assign bitwise XOR Assign bitwise OR Assign left shift Assign right shift |
Right to left |
| , | Separator of expressions | Left to right |
Associativity of Operators
The associativity of operators determines the direction in which an expression is evaluated. For example,
b = a;
Here, the value of a is assigned to b, and not the other way around. It's because the associativity of the = operator is from right to left.
Also, if two operators of the same precedence (priority) are present, associativity determines the direction in which they execute.
Let us consider an example:
1 == 2 != 3
Here, operators == and != have the same precedence. And, their associativity is from left to right. Hence, 1 == 2 is executed first.
The expression above is equivalent to:
(1 == 2) != 3
Note: If a statement has multiple operators, you can use parentheses () to make the code more readable.