1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how???

Discussion in 'C' started by IndiraP, Dec 12, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    I have seen this one in this forum only..but i couldnt understand how come the answer is 10 for the below ???

    int z,x=5,y=-10,a=4,b=2;
    z = x++ - --y * b / a;

    then output of z..???
     
  2. Rajesh M. Kanojia

    Rajesh M. Kanojia New Member

    Joined:
    Dec 9, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    INDIA
    i think the value of z is 10.
    how:-
    step 1:- --y is evaluated which become -11.
    step 2:- (-11) will multiple with b which become -22
    step 3:- this (-22) get devided by a now intermidiate result will be -5
    step 4:- now expression become as z=x++ - -5;
    step 5 :- -5 become +5
    step 6:- here the main game start the priority of post-increment is lower then assignement operater that's why first the value of x which is 5 is added with intermidiat result +5 which become 10 and this 10 get assigne into variable z .
    step 7:- variable x increment its value by 1 and become 6.
     
  3. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    But ++, -- have higher precedence than / ,*..???
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Don't forget that x++ means POSTincrement and ++x means PREincrement. That means before and after, and is usually before or after some execution point. So the equation in some senses (but definitely not all) is equivalent to:
    Code:
    --y;
    z = x - y * b / a;
    x++;
    
    This should be a lot easier to evaluate. First y is decremented to -11.
    Then z=5 - -11 * 2 / 4
    =5 - -22/4
    =5 - -5
    =5 + 5
    =10.

    Then x is incremented.
     
  5. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Thank u sir... :)
     

Share This Page