C++ operator precedence and associativity


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 every expression if more than one operators is involve there is a certain rule the compiler must follow on which operators to execute first and which operators second and so on.In C++ precedence and associativity determines the order of priority in which the operators get executed.So whenever we say precedence and associativity of an operator we simply mean it’s level of executionary importance in a hierarchy of priority order of operators.Precedence and associativity are discussed in more detail below.

Operator 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 values of ‘result’.The first result is 25.2273 when ‘/‘(division) is performed first and the second output is 2.75 when ‘+‘ is performed first.But if you run the program you will get the value of result as 25.2273 which means division is performed first which also means that ‘/’ has higher precedence than ‘+’.Thus precedence denotes the order in which an operation is performed.You can see the list of the order of precedence for all the operators below.



Associativity

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

#include &at;iostream>

using namespace std;

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

cout&at;&at; result &at;&at; endl ;

cin.get( ) ;
return 0 ;
}

‘*’ and ‘/’ have the same precedence but if we look at the table below the associativity is from left to right:which means the associativity increases from left to right.This means the priority of ‘/’ is more than ‘*’ and so division is performed first.Hence the expression f1*f2/f3*f1 is interpreted as (f1* (f2/f3) *f1) and so the result is 1142.16.Please check out the precedence and associativity table given below for all the operators.

Precedence and associativity table
Precedence and associativity table.png


 


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 ;
}

 


Related Link

->Precision and accuracy in float.

->Using wchar_t to print out Japanese,Chinese,Russian in Console.