Precedence and Associativity in C++

Precedence and Associativity

 

  • The sequence of operators and operands that reduces to a single value after the evaluation is called an expression
  • If 2*3 is evaluated nothing great ,it gives 6 but if 2*3+2 is 6+2 =8 or 2*6=12.To avoid this confusion rules of  Precedence and associativity are used
precedence and associativity in c++

Precedence

Operator precedence gives priorities to operators while evaluating an expression

Example: when 2*3+2 is evaluated  output is 8 but not 12 because the * operator is having more priority than + hence 2*3 is  evaluated first followed by 6+2.

Operator precedence table

  • Operator precedence table gives the detail list of priorities for each and every operator
  • Operators are listed from higher priority to lower
PrecedenceOperatorDescriptionAssociativity
1::Scope resolutionLeft-to-right
2++   --Suffix/postfix increment and decrement
type() type{}Function-style typecast
()Function call
[]Array subscripting
.Element selection by reference
->Element selection through pointer
3++   --Prefix increment and decrementRight-to-left
+   -Unary plus and minus
!   ~Logical NOT and bitwise NOT
(type)C-style type cast
*Indirection (dereference)
&Address-of
sizeofSize-of
newnew[]Dynamic memory allocation
deletedelete[]Dynamic memory deallocation
4.*   ->*Pointer to memberLeft-to-right
5*   /   %Multiplication, division, and remainder
6+   -Addition and subtraction
7<<   >>Bitwise left shift and right shift
8<   <=For relational operators < and ≤ respectively
>   >=For relational operators > and ≥ respectively
9==   !=For relational = and ≠ respectively
10&Bitwise AND
11^Bitwise XOR (exclusive or)
12|Bitwise OR (inclusive or)
13&&Logical AND
14||Logical OR
15?:Ternary conditional operatorRight-to-left
=Direct assignment (provided by default for C++ classes)
+=   -=Assignment by sum and difference
*=   /=   %=Assignment by product, quotient, and remainder
<<=   >>=Assignment by bitwise left shift and right shift
&=   ^=   |=Assignment by bitwise AND, XOR, and OR
16throwThrow operator (for exceptions)
17,CommaLeft-to-right

Example1

Evaluate 5*4+(3+2)

  • Parenthesis is having the highest priority
    5*4+5
  • Among * and +,* is having the highest priority
    20 + 5= 25 is the final output

Example 2

Arithmetic operators for having higher priority than relational operators

Observe the difference With Parenthesis  and without Parenthesis

5<4+5<4 
5<9<4// 5<9 gives 1
1<4 =1
(5>4)+(5>4)//5>4 is true 
1+1
2

Associativity

Associativity determines in which direction operators  are to be evaluated if they are under the same priority i.e

  1. Left to Right
  2. Right to Left

Example

5*3/2
15/2
7

Here * and / are having the same level of priority but it associativity is given as left to right. hence evaluate* followed by /

Note

From the Operator precedence table, we can conclude  except for   group 3 and group 15 all the remaining operators are having associativity as,left to right

C++ program to demonstrate operator precedence and associativity

main() 
{
int a = 20,b = 10,c = 15 d = 5,e;

e = (a + b) * c / d; // ( 30 * 15 ) / 5
cout << "Value of (a + b) * c / d is :" << e << endl ;

e = ((a + b) * c) / d; // (30 * 15 ) / 5
cout << "Value of ((a + b) * c) / d is :" << e << endl ;

e = (a + b) * (c / d); // (30) * (15/5)
cout << "Value of (a + b) * (c / d) is :" << e << endl ;

e = a + (b * c) / d; // 20 + (150/5)
cout << "Value of a + (b * c) / d is :" << e << endl ;

}


 

2 comments on “Precedence and Associativity in C++”