precedence and associativity
Geeks4Geeks: Operator Precedence and Associativity
Programiz: C Precedence And Associativity Of Operators
Precedence and Associativity
These are rules that decide the order upon which parts of the overall expression are evaluated.
Precedence
Tells us which operators are evaluated first.
Associativity
For operators with the same precedence, associativity (or left-right or right-left or "left-to-right" or "right-to-left" associativity) tells us in which direction is an expression evaluated.
Full Table
Operator | Precedence | Associativity |
---|---|---|
() - function calls[] - arrays. - dot operator-> - structure pointers++ , — - postfix increment or decrement |
1 (first priority or highest precedence) | Left-to-Right |
++ / — - prefix increment or decrement+ / – - unary plus or minus! , ~ - logical NOT, bitwise component(<type>) - typecasting/cast operator* - dereference operator (pointers)& - addressof operator (pointers)sizeof - determine size of in bytes |
2 | Right-to-Left |
*,/,% - multiplication, division, or modulus operations |
3 | Left-to-Right |
+/- - addition or subtraction |
4 | Left-to-Right |
<< , >> - Bitwise shift left or Bitwise shift right |
5 | Left-to-Right |
< , <= - relational less than, less than or equal to> , >= - relational greater than, greater than or equal to |
6 | Left-to-Right |
== , != - Relational is equal to, is not equal to | 7 | Left-to-Right |
& - bitwise AND |
8 | Left-to-Right |
^ - Bitwise exclusive OR (caret) |
9 | Left-to-Right |
| - (pipe only) Bitwise inclusive OR |
10 | Left-to-Right |
&& - logical AND |
11 | Left-to-Right |
|| - (double pipe) logical OR |
12 | Left-to-Right |
?: - ternary conditional |
13 | Right-to-Left |
= - assignment operator+= , -= - addition/subtraction assignment*= , /= - multiplication/division assignment%= , &= - modulus/bitwise AND assignment^= , |= - bitwise exclusive and inclusive OR assignment<<=, >>= - bitwise shift left and shift right assignment |
14 | Right-to-Left |
, - (comma) expression separator |
15 (lowest precedence) | Left-to-Right |
Source: Geeks4Geeks Operator Precedence and Associativity in Programming |