## What is precedence and associativity of C operators

Precedence and associativity is the priority of operators to perform it’s operation. We have to use a number of operators when we write a program. Operators precedence determines that which operator will work first. Let’s try to understand with an example of precedence and associativity.

Suppose, you have to solve the following problem.

10 * 5 + 9 / 3 -7 = ?

Now, what you will do? We know that the * and / operators will works first. Then we will simplify this program by following.

50 + 3 – 7

= 53 – 7

= 46

But, if we perform + and – operator first, then we will get different result which is not correct.

10 * 5 + 9 / 3 – 7

= 10 * 14 / (-4)

= – 35   ( which is wrong)

In programming language, precedence of operators determine that which operator will work first. This is called precedence of operator.

But, if the precedence of two or more operators are same, then we should consider the associativity. Associativity of operators determines that whether they will work left to right or right to left.

Different operators have different precedence and associativity. Here we are giving a table of precedence and associativity of our generally used operators.

## Table of precedence and associativity

Operators Description Precedence Associativity
( )
Function expression
01
Left to Right
[ ]
Array
02
Left to Right
->
Structure operator
03
Left to Right
-
Unary minus
04
Right to Left
++ --
Increment Decrement
05
Right to Left
~
One's complement
06
Right to Left
& *
Pointer
07
Right to Left
(data type)
Type cast
08
Right to Left
sizeof
sizeof operator
09
Right to Left
>> <<
Right and Left shift
10
Left to Right
* / %
Multiplication, division and reminder operator
11
Left to Right
+ -
12
Left to Right
<
Less than
13
Left to Right
>
Greater than
14
Left to Right
<=
Less than or equal
15
Left to Right
>=
Greater than or equal
16
Left to Right
==
Equal to operator
17
Left to Right
!=
Not equal to operator
18
Left to Right
&&
Logical AND operator
19
Left to Right
||
Logical OR operator
20
Left to Right
!
Logical NOT operator
21
Right to Left
&
Bitwise AND operator
22
Left to Right
^
Bitwise EX-OR operator
23
Left to Right
|
Bitwise OR operator
24
Left to Right
=
Assignment operator
25
Right to Left
*=
Assignment operator
26
Right to Left
/=
Assignment operator
27
Right to Left
%=
Assignment operator
28
Right to Left
+=
Assignment operator
29
Right to Left
-=
Assignment operator
30
Right to Left
&=
Assignment operator
31
Right to Left
^=
Assignment operator
32
Right to Left
|=
Assignment operator
33
Right to Left
<<=
Assignment operator
34
Right to Left
>>=
Assignment operator
35
Right to Left
,
Comma operator
36
Right to Left
?:
Conditional operator
37
Right to Left

## Example program to learn precedence and associativity in C language

Here, we have given a C program to understand precedence and associativity. Try to understand it. Although we have used the same operators and value here, but we have got different result every time for precedence and associativity.

``````#include <stdio.h>
main(){
int a = 30;
int b = 20;
int c = 10;
int d = 5;
int result;

result = (a - b) * c / (d - a);    // (30 - 20) * 10 / (5 - 30) = -4
printf("%d\n", result);

result = ((a - b) * c) / d - a;    // ((30 - 20) * 10) / 5 - 30 = -10
printf("%d\n", result);

result = a - b * c / d - a;    // 30 - 20 * 10 / 5 - 30 = -40
printf("%d\n", result);

result = a - (b * c) / (d - a);    // 30 - (20 * 10) / (5 - 30) = 38
printf("%d\n", result);

return 0;
}``````

Output of this program 