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.

Similar Reads

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

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

Operator Precedence and Associativity Table:

The following tables list the operator precedence from highest to lowest and the associativity for each of the operators:...

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