Translate

Labels

Tuesday 13 August 2013

C PUZZLES

Basic Arithmetic Operators

mian()
{
int x;
x=- 3 + 4 * 5 -6;    /*1*/
printf("%d\n",x);
x=3 + 4 % 5 - 6;     /*2*/
printf("%d\n",x);
x=- 3 *4 %-6 / 5;    /*3*/
printf("%d\n",x);
x=(7+6)%5/2;         /*4*/
printf("%d\n",x);
}


1.x=- 3 + 4 * 5 -6;             by reading the precedence table

   x=(- 3) + 4 * 5 -6;          The highest level operator in the expression is the unary -. We'll use                                                                parentheses indicate the order of binding operands to operators. 
   x=(- 3) + (4 * 5) -6;        Next highest in the expression is *.

  
x=((- 3) + (4 * 5)) -6;     Both + and - are at the same precedence level.The order  of   binding thus depends on the associativity rule for that level.For + and -,associativity is left to right.First the + is bound.

   x=(((- 3) + (4 * 5)) -6);   And then the -.

   ( x=(((- 3) + (4 * 5)) -6)); And finally near the bottom of the precedence table is =. Now that we have completely identified the operands for each operator,we can evaluate the expression.
   ( x=(((- 3) + (4 * 5)) -6));  For this expression,evaluation proceeds from inside out.
   
   (x=((-3+20)-6))                 Replace each sub expression by its value.
   
    (x=(17-6))
   
    (x=11)

     11                                   The value of an assignment expression is the  value of the right-hand side cast in the type of the left-hand side.


2.x=3 + 4 % 5 - 6                This expression is very similar to the previous one.

   x=3 + (4 % 5) - 6              Following precedence

   x=3 + (4 % 5) - 6             and associativity

   x=3 + (4 % 5) - 6             leads to 

  (x=3 + 4 % 5 - 6)              this.(The module,%,operator yields the remainder of dividing 4 by 5)

 (x=((3+4)-6))                     Again, evaluation is from the inside out.

 (x=(7-6))

 (x=1)
 1


 3.x= - 3 * 4 % - 6 / 5            This expression is a bit more complex than the last,but rigorous adherence to precedence and associativity will untangle it.
  
    x= (- 3) * 4 % (- 6) / 5

    x= ((- 3) * 4) % (- 6) / 5    *,%,and / are all the same precedence level,and they associate from left to right.

    x= (((- 3) * 4) % (- 6)) / 5

    x= ((((- 3) * 4) % (- 6)) / 5)

    (x= ((((- 3) * 4) % (- 6)) / 5))

   (x=(((-3*4)%6)/5))               Evaluating from the inside out.

   (x=((-12%-6)/5))

   (x=(0/5))
    
   (x=0)

   0



 4.x=(7 + 6) % 5 / 2              Of course we are not totally at the mercy of predefined precedence. Parentheses can always be used to effect or clarify a meaning.
   
    x=(7 + 6) % 5 / 2              Sub expression within parentheses bind first.
    
    x=((7 +6) % 5 /2)              Then it is according to the precedence and associativity rules as before.

    (x=(((7 + 6) % 5) / 2))

    (x=((13%5)/2))                 Evaluating

    (x=(3/2))

    (x=1)                               Integer arithmetic any fractional part.

    1.

No comments: