C Operator Precedence and Associativity
Precedence of Operators in C:
Operator precedence tells
the order, in which operations are performed in an expression.
For
example:
int n = 200 + 10 * 50;
Would the above expression
be evaluated as (200 + 10) * 50 or 200 + (10 * 50). Would the addition be
done first or multiplication. We all have studied BODMAS rule in mathematics, thus, we know that multiplication is
done before addition. So the above expression would be solved like this given
below-
Step
1 : 200 + (10 * 50)
Step
2 : 200 + 500
Result
: 700
The precedence can be
changed using parenthesis. The sub-expression in parenthesis would be evaluated
first, no matter what operator has been used outside the sub-expression.
For
example:
int n = (200 + 10) * 50;
This expression would be
solved like this as given below-
Step
1 : 210 * 50;
Result
: 10500
If nested parenthesis has
been used, then innermost parenthesis would be evaluated first.
For
example:
int n = 2 + (3 + (5 – 1)) * 4;
This expression would be
evaluated like this as given below-
Step
1 : 2 + (3 + 4) * 4
Step
2 : 2 + 7 * 4
Step
3 : 2 + 28
Result
: 30
If there is more than one
parenthesis at different places in an expression, then sub-expressions in the
parenthesis would be evaluated from left to right.
Associativity of Operators in C:
In C programming language,
many operators can have same precedence or priority. If, in an expression, more
than one operator have same precedence, the problem is resolved using the associativity of operators. All operators
in C have either ‘Left to Right’ or
‘Right to Left associativity. It
means, in an expression, if two operators have same precedence or priority, and
their associativity is ‘Left to Right’, the operator, which comes first from
left to right would be evaluated first. Let us understand this with an example
given below-
int n = 5 + 3 * 4 / 2 – 1
This expression would be
evaluated like this as given below-
Step
1 : 5 + (3 * 4) / 2 – 1
Step
2 : 5 + 12 / 2 – 1
Step
3 : 5 + (12 / 2 ) – 1
Step
4 : 5 + 6 – 1
Step
5 : (5 + 6) – 1
Step
6 : 11 – 1
Result
: 10
In the above example,
operators ‘*’ and ‘/’ have the same precedence. Their
associativity is from left to right. Therefore, multiplication would take place
before division. The same way, operators ‘+’
and ‘-‘ have same precedence, and
associativity ‘Left to Right’. Hence, addition will be done before subtraction.
Again, precedence can be
changed using parenthesis.
The precedence and
associativity table of all operators in C has been given below for your quick
reference. Operators are given from top to bottom in descending precedence or
priority.
Precedence
|
Operator
|
Description
|
Associativity
|
1
Highest
|
++
|
Postfix increment
|
Left-to-right
|
--
|
Postfix decrement
|
||
()
|
Function call
|
||
[]
|
Array subscripting
|
||
.
|
Element selection by
reference
|
||
->
|
Element selection
through pointer
|
||
2
|
++
|
Prefix increment
|
Right-to-left
|
--
|
Prefix decrement
|
||
+
|
Unary plus
|
||
-
|
Unary minus
|
||
!
|
Logical NOT
|
||
~
|
Bitwise NOT (One's
Complement)
|
||
(type)
|
Type cast
|
||
*
|
Indirection
(dereference)
|
||
&
|
Address-of
|
||
sizeof
|
Size-of
|
||
3
|
*
|
Multiplication
|
Left-to-right
|
/
|
Division
|
||
%
|
Modulo (remainder)
|
||
4
|
+
|
Addition
|
Left-to-right
|
-
|
Subtraction
|
||
5
|
<<
|
Bitwise
left shift
|
Left-to-right
|
>>
|
Bitwise right shift
|
||
6
|
<
|
Less than
|
Left-to-right
|
<=
|
Less than or equal to
|
||
>
|
Greater than
|
||
>=
|
Greater than or equal
to
|
||
7
|
==
|
Equal to
|
Left-to-right
|
!=
|
Not equal to
|
||
8
|
&
|
Bitwise AND
|
Left-to-right
|
9
|
^
|
Bitwise XOR (exclusive
or)
|
Left-to-right
|
10
|
|
|
Bitwise OR (inclusive
or)
|
Left-to-right
|
11
|
&&
|
Logical AND
|
Left-to-right
|
12
|
||
|
Logical OR
|
Left-to-right
|
13
|
?:
|
Ternary
conditional
|
Right-to-left
|
14
|
=
|
Direct assignment
|
Right-to-left
|
+=
|
Assignment by sum
|
||
-=
|
Assignment by
difference
|
||
*=
|
Assignment by product
|
||
/=
|
Assignment by quotient
|
||
%=
|
Assignment by
remainder
|
||
<<=
|
Assignment by bitwise
left shift
|
||
>>=
|
Assignment by bitwise
right shift
|
||
&=
|
Assignment by bitwise
AND
|
||
^=
|
Assignment by bitwise
XOR
|
||
|=
|
Assignment by bitwise
OR
|
||
15
lowest
|
,
|
Comma
|
Left-to-right
|
This is all about precedence
and associativity in C computer language. All operators will be described in
later posts, wherever they are used.
If you are interested in how
to create simple graphics in C, please visit my blog Graphics in C.