Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how??? (http://www.go4expert.com/forums/how-t29343/)

IndiraP 12Dec2012 18:23

how???
 
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..???

Rajesh M. Kanojia 12Dec2012 22:19

Re: how???
 
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.

IndiraP 12Dec2012 22:27

Re: how???
 
But ++, -- have higher precedence than / ,*..???

xpi0t0s 13Dec2012 04:26

Re: how???
 
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.

IndiraP 13Dec2012 08:28

Re: how???
 
Quote:

Originally Posted by xpi0t0s (Post 98428)
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.

Thank u sir... :)


All times are GMT +5.5. The time now is 08:34.