Difference between Operator Precedence and Operator Associativity
In programming, operators are used to perform various operations on data. Understanding how operators interact with each other is crucial for writing correct and efficient code. In this article, we will explore the two most important concepts which are Operator Precedence and Operator Associativity.
Operator Precedence:
Operator precedence determines which operation is performed first in an expression with more than one operator with different precedence. Operators with higher precedence are evaluated before those with lower precedence.
Example of Operator Precedence:
Let’s try to evaluate the following expression,
10 + 20 * 30
The expression contains two operators, + (plus), and * (multiply).The multiplication operator * has higher precedence than + so, the first evaluation will be
10 + (20 * 30)
After evaluating the higher precedence operator, the expression is
10 + 600
Now, the + operator will be evaluated.
610
Operator Associativity:
Operator associativity defines the direction in which operators of the same precedence are evaluated when they appear in an expression. It can be either left-to-right or right-to-left.
Example of Operator Associativity:
Let’s evaluate the following expression,
100 / 5 % 2
Both / (division) and % (Modulus) operators have the same precedence, so the order of evaluation will be decided by associativity.
The associativity of the multiplicative operators is from Left to Right. So,
(100 / 5) % 2
After evaluation, the expression will be
20 % 2
Now, the % operator will be evaluated.
0
Operator Precedence and Associativity Table:
The following tables list the operator precedence from highest to lowest and the associativity for each of the operators:
Precedence |
Operator |
Description |
Associativity |
---|---|---|---|
1 |
() |
Parentheses (function call) |
Left-to-Right |
[] |
Array Subscript (Square Brackets) |
||
. |
Dot Operator |
||
-> |
Structure Pointer Operator |
||
++ , — |
Postfix increment, decrement |
||
2 |
++ / — |
Prefix increment, decrement |
Right-to-Left |
+ / – |
Unary plus, minus |
||
! , ~ |
Logical NOT, Bitwise complement |
||
(type) |
Cast Operator |
||
* |
Dereference Operator |
||
& |
Addressof Operator |
||
sizeof |
Determine size in bytes |
||
3 |
*,/,% |
Multiplication, division, modulus |
Left-to-Right |
4 |
+/- |
Addition, subtraction |
Left-to-Right |
5 |
<< , >> |
Bitwise shift left, Bitwise shift right |
Left-to-Right |
6 |
< , <= |
Relational less than, less than or equal to |
Left-to-Right |
> , >= |
Relational greater than, greater than or equal to |
||
7 |
== , != |
Relational is equal to, is not equal to |
Left-to-Right |
8 |
& |
Bitwise AND |
Left-to-Right |
9 |
^ |
Bitwise exclusive OR |
Left-to-Right |
10 |
| |
Bitwise inclusive OR |
Left-to-Right |
11 |
&& |
Logical AND |
Left-to-Right |
12 |
|| |
Logical OR |
Left-to-Right |
13 |
?: |
Ternary conditional |
Right-to-Left |
14 |
= |
Assignment |
Right-to-Left |
+= , -= |
Addition, subtraction assignment |
||
*= , /= |
Multiplication, division assignment |
||
%= , &= |
Modulus, bitwise AND assignment |
||
^= , |= |
Bitwise exclusive, inclusive OR assignment |
||
<<=, >>= |
Bitwise shift left, right assignment |
||
15 |
, |
comma (expression separator) |
Left-to-Right |
Difference between Operator Precedence and Operator Associativity:
Aspect | Operator Precedence | Operator Associativity |
---|---|---|
Definition | Determines the order of evaluation of operators in an expression. | Defines the direction of evaluation when operators of the same precedence appear. |
Priority | Higher precedence operators are evaluated before lower precedence operators. | Associativity determines the order in which operators are evaluated within an expression of the same precedence. |
Example | In 2 + 3 * 4 , * has higher precedence than + , so 3 * 4 is evaluated first. |
In 2 + 3 + 4 , left-associative operators are evaluated from left to right. |
Representation | Precedence levels are often specified in language documentation or expressed with parentheses in expressions. | Associativity is usually implicit and is determined by the language specification. |
Contact Us