how???

IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
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..???
0
Rajesh M. Kanojia's Avatar, Join Date: Dec 2012
Go4Expert Member
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.
0
IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
But ++, -- have higher precedence than / ,*..???
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
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...