6.1 – Operators precedence and associativity

by subbu on September 25, 2013

Operators in C language:

We know that an operator is a symbol that tells the computer to perform some action on constants and variables called operands. So far we have used some of the operators like arithmetic, relational, logical, assigning and conditional operators. In this session we are going to discourse some of the important terms used along with operators like unary, binary, priority/precedence and associativity of operators.

Precedence of operators:

An expression is a combination of operators, constants and variables. CPU never executes the total expression at once, evaluates operator by operator according to their precedence. According to the language specification every operator has a level of precedence some operators get higher priority, some may get lower priority and some get equal priority. We must keep precedence of operators in mind while writing any expressions.

x=4+(12-6)*5;

Precedence of operators used in the above statement from high to low are () * + =. The statement would be executed step by step as

x=4+(12-6)*5   // () 1st
x=4+6*5        //  * 2nd
x=4+30         //  + 3rd
x=43           //  = 4th

Associativity of operators:

If multiple operators with the same precedence are used in an expression then associativity rule tells the compiler which one has to execute first and which one has to execute next that is from left to right or right to left.

x=4/2+3*5%7;

In the above statement * / % have the same precedence and with associativity from left to right. Hence / executes first, * second and % last. The statement would be executed step by step as

x=4/2+3*5%7       //  / 1st
x=2+3*5%7         //  * 2nd
x=2+15%7          //  % 3rd
x=2+1             //  + 4th
x=3               //  = 5th
x=y=z=10;

In the above statement assigning operator is used for number of times, its associativity is from right to left. Hence 10 assigns to z first, z assigns to y second and finally y assigns to x.

Unary operators in C language:

Some operators like -, +, ~, ++, – -, !, *, &, sizeof and cast operator are unary operators in C language. These operators are used beside a single variable, constant or expression so called unary operators.

#include<stdio.h>
int main()
{
int a=10,b;
b=-a;                 /*Unary -  */
printf("%d",b);
return 0;
}

Output:
-10

#include<stdio.h>
int main()
{
int x;
x=!0;                /* Not false is true (1) */
printf("%d",x);
return 0;
}

Output:
1

#include<stdio.h>
int main()
{
int x;
x=-(4+5*2);       /* Unary - with expression */
printf("%d",x);
return 0;
}

Output:
-14

Binary operators in C language:

All operators other than unary operators are called binary operators. These operators are used in between any two operands.

#include<stdio.h>
int main()
{
int x,y;
x=5*6/15;                   /* binary arithmetic operators */
y=45>30&&60!=40;     /* binary relational operators  */
printf("%d\n%d",x,y);
return 0;
}

Output:
2
1

All the operators available in C language with their precedence and associativity are summarized in the following table.  These will be discoursed in length in future sessions.

Operator Description Associativity
()
[] ->
.
Function Expression
Array Expression
Pointer to
Direct member access

Left to Right


++  – –
~
!
&
*
(type)
sizeof
Unary minus
Increment/Decrement
One’s compliment
Negation
Address of
Value at
Typecast
Size in bytes

Right to left

*
/
%
Multiplication
Division
Modulus

Left to right

+
Addition
Subtraction

Left to right

<<
>>
Left shift
Right shift

Left to right

<
>
<=
>=
Less than
Greater than
Less than or equal to
Greater than or equal to

Left to right

==
!=
Equal to
Not equal to

Left to right

& Bitwise AND

Left to right

^ Bitwise exclusive OR

Left to right

| Bitwise inclusive OR

Left to right

&& Logical AND

Left to right

|| Logical OR

Left to right

? : Conditional

Right to left

=
*=    /=   %=
+=   -=    &=
^=   |=
<<=  >>=
Assignment

Right to left

, Comma

Right to left

Previous post:

Next post: