C++ operator precedence and associativity

In C++ operator precedence and associativity are terms related to the order in which an operators perform the operation.In a complex expression where more than one operators is involve there is a certain rule the compiler must follow on how to perform the operation.Using this simple rule the compiler determine which operator to execute first and which operators second and so on.In C++ precedence and associativity determines the order or priority in which the operators get executed.So whenever we say precedence and associativity of an operator we simply refers to the hierarchy in which the operators are executed.Precedence and associativity are discussed in more detail below.

i)Precedence
ii)Associativity


i)Precedence

Precedence denotes the order in which an operator perform the operation.In mathematical evaluation an expression may contain many operators in such case knowing the order of the operators will help us get the correct value.For example consider the program below.

#include <iostream>

using namespace std ;

int main( )
{
float f1=23 , f2=98 , f3=44 ;
float result=f1 + f2/f3 ;

cout<< result << endl ;

cin.get( ) ;
return 0 ;
} 

There are two possible output here,
 
i)The first output is 25.2273 when ‘/‘(division) is performed first and
ii)the second output is 2.75 when ‘+‘ is performed first.
 
But if you run the program you will get the value of ‘result'(output) as 25.2273 which means division is performed first.Now this means division operator has higher level than ‘+’(addition operator).This also mean division has higher precedence than addition operator.Thus precedence denotes the order in which an operator perform the operation.You can see the list of the order of precedence for all the operators below i.e after associativity is dsicussed.


ii)Associativity

In a mathematical expression sometimes the operators may have the same precedence in such case associativity will determine the order of execution of the operators.So associativity denotes the order in which operators with the same precedence is evaluated.Consider the program below.

#include <iostream>

using namespace std;

int main( )
{
float f1=23 , f2=95 , f3=44 ;
float result=f1* f2/f3 *f1 ;

cout<< result << endl ;

cin.get( ) ;
return 0 ;
} 

What is the output here is it,

i)1142.16 where the division is performed first and then the multiplication or
ii)2.15909 when multiplication is performed and then division.

To know which the output look at the Precedence and associativity table given below.

If we look at the table below(the 5th row) ‘*’ and ‘/’ have the same precedence,this mean they have the same level of operation.Since they have the same level of operation we will use it’s asscociativity to determine which will be operated first.And note associativity order is from left to right meaning as we move towards right the execution order increases(it is also shown in the second column in the table).Now look at the ‘Mulplicative’ column and it’s corresponding row,the (division)’/’ lies towards right of ‘*'(multiplication opeartor) ,so ‘/’ has higher associativity meaning ‘/’ will be performed first.Now since we know that ‘/’ has higher priority so in the above program the division operation will be performed first hence the output is 1142.16.

 
Precedence and Associativity table

 


An example for you!

Using the table above try to predict the output of the program given below.

#include <iostream>

using namespace std;

int main( )
{
int i1=9 ;
int i2= – -i1 * – -i1;

cout<< i2 << endl ;

cin.get( ) ;
return 0 ;
}

 


Side note

Here is the two most important things or two important things that will help you remember which operators has higher precedence than which.

i)Every logical operator has lower precedence than every relational operator.
 
ii)The shift operators has more precedence than the relational operators but less precedence than the arithmetic operators.




Leave a Reply

Your email address will not be published. Required fields are marked *