Open In App

Operator Precedence and Associativity in Programming

We have many types of operators in a programming language. Operators serve many purposes. But when we have many operators within the same statement, Operator Precedence and Associativity come into the picture. They define which operator should be taken into account first. It is similar to the BODMAS rule we have learned in our school mathematics. In this article, we will see what is Operator Precedence and Associativity.

Operators Precedence:

When various operators are used within the same statement a problem arises which is which operation should be performed first. To solve this problem there is the concept of operator precedence. Operator Precedence is a set of rules that defines the priority for all the operators present in a programming language. The operation which has an operator with the highest priority will be executed first.



Example:

a= 4+5/10;
In the above given example if we solve the addition first then the answer will be 9/10=0.9 .
Now if we solve the division first then the answer will be 4 + 0.5 = 4.5 .
Out of this the second answer is a valid answer because the precedence of division is more than addition.
Therefore a=4.5

Operator Associativity:

If we have operators which have same precedence in the same statement then we use associativity to figure out which operation should be performed first. Associativity defines whether you should go from left to right or you should go to right to left. If Associativity says that you should go left to right then operation on the left side should be performed first and then only we should move to right side.



Example:

a= 5*5/10
If we solve the multiplication operation first then answer will be 25/10= 2.5
If we solve the division operation first then the answer will be 5*0.5=2.5
Out of this the first answer is correct because the given two operators have same precedence therefore we should look at the
Associativity which is left to right.
Therefore , a=2.5 .

Following table define the Precedence and Associativity for all the operators in a programming language.

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


Article Tags :