Thursday, October 24, 2013

Priority of operators

 Do you remember the BODMAS rule of mathematics? Sorry… it’s VBODMAS actually.

V        :Vinculum             1st priority
B        :Bracket                2nd priority
O       :Of                        3rd priority
D       :Division               4th priority
M       :Multiplication       5th priority
A        :Addition               6th priority
S        :Subtraction          7th priority

So all time, whenever you solve mathematics expressions you have to follow this priority. Keep in mind that parentheses, and braces are used more frequently instead of vinculum now a days.

In the same way, if you deal with C’s arithmetic expressions, then also you have to follow certain rules. The rules which decides the priority of operators. 

Consider the following expression:
a*b+c
should the expression a*b+c be evaluated by performing the multiplication first or by preforming addition first?
          (a*b)+c or
          a*(b+c)

C solves this problem by assigning priorities to operators. Operators with high priority evaluates first than the operator having low priority. The following table shows the priority of C’s arithmetic operators.

Operator                                 Priority
()                                             1st
*        /        %                         2nd
+       -                                     3rd
=                                              4th     
Thus, a+b*c is evaluated as:
a+(b*c)

As the multiplication(*)  has the higher priority than addition(+). It the addition(+) was to be evaluated first then we need to use brackets as follows:
(a+b)*c

Note that the operator % is not indicates percentage. It is modulus operator.  Before proceeding any further it is necessary to know about it.

¨ Modulus operator(%) returns reminder after division.
E.g. 12%2=0
       5%2=1

 ¨ The sign of the first operand will be the sign of result.
 E.g. -5%2 =-1
       -5%-2=-1
        5%-2= 1

¨ If the value of numerator is less than that of denominator then result will be the   
   value of numerator.

E.g. 9%10=9   
       Here numerator 9 is less than denominator 10 so the result will be 9.
      1%10=1

¨ It works on integers only.
         
Examples:
Evaluate the following expressions using priority of operators.
     1)   a=100/2*1/2+5/2+99
     2)   b=9+7/2
     3)   c=-3+-3+9*2*3/9
     4)   d=3/5/4/2-3+2%9
     5)   e=-5%3+8/9%10+2


1) a=100/2*1/2+5/2+99
      =50*1/2+5/2+99
      =50/2+5/2+99
      =25+5/2+99
      =25+2+99
      =126

Note: Observe that, if the operators of same priority occur from left to right then priority is given on the first come first serve basis.

In the above expression the result of expression 5/2=2 and not 2.5. As integer and integer always yields an integer result. So the result of the expression 1/2=0 and not 0.5. If you want the result you expected then, one of the operands must be of type float.
E.g.
                  5/2.0    = 2.5
                  5.0/2   = 2.5
                  5.0/2.0 = 2.5

2) b=9+7/2+9/9%30+3
      =9+3+9/9%30+3
      =9+3+1%30+3
      =9+3+1+3
      =16

3) c=-3+ -2+9*2*3/9
      =-3+ -2+18*3/9
      =-3+ -2+54/9
      =-3+-2+6
      = -5+6
      =1

4) d=3/5/4/2-3+2%9
      =0/4/2-3+2%9 
      =0/4-3+2%9
      =0-3+2%9
      =0-3+2
      =-3+2
      =-1

5) e=-5%3+8/9%10+2
      =-2+8/9%10+2 
      =-2+0%10+2
      =-2+0+2
      =0


Associativity: Operators on the same level have the same precedence. The order of the execution of operators on the same level is controlled by associativity. Left associative operators are evaluated from left to right. Right associative operators are evaluated from right to left.
Look at the following example:

#include <stdio.h>
#include <conio.h>
main()
{
 int i=10;
 clrscr();
 printf("%d %d %d",i++,i++,i++);
 getch();

}

Output:
#-------------------------------------------------------------------------------#
12 11 10
#-------------------------------------------------------------------------------#
Note that, the associativity of ++ operator is from right to left. So first the value of last i++ gets printed i.e. 10. As ++ is post increment operator, first the value of i gets printed and then it is incremented by 1. So, the value of second last i++ i.e. 11 is printed and lastly the value of first i++ i.e. 12 gets printed. 

No comments:

Post a Comment