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
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.