468,484 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

prefix , postfix operator associativity

hey all ! i am pragensh and joined today for follwoing reason
can you help me how this output came ?
x=3;
x-=--x-x--;
printf("x=%d",x);
here answer is coming as 1 how come ?
can you please help me to find the reason ? or
step wise explaination
Sep 14 '06 #1
3 3469
D_C
293 100+
Putting two unary operations like that on the same line has undefined behavior, it's not ever a good idea. However, in your case, it looks like your compiler evaluates left to right. However, parenthesis come first. I substitute for x when I put it's value in parenthesis, i.e. --(3), (2)--, or (1).

x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 2)
x = (1) - 0
x = 1 - 0
x = 1
Sep 14 '06 #2
Putting two unary operations like that on the same line has undefined behavior, it's not ever a good idea. However, in your case, it looks like your compiler evaluates left to right. However, parenthesis come first. I substitute for x when I put it's value in parenthesis, i.e. --(3), (2)--, or (1).

x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 2)
x = (1) - 0
x = 1 - 0
x = 1
thank you sir but i am get back to you with some doubt regarding the same topic
Sep 15 '06 #3
sir i think this is how the steps will go,but can u explain me your logic in steps (1) & (2)


x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 1) /* (sir according to postfix operation) */ .. (1)
x = (2) - 1 (2)
x = 2-1
x = 1
Jan 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Sergey | last post: by
2 posts views Thread by shan | last post: by
8 posts views Thread by subramanian100in | last post: by
3 posts views Thread by news.aioe.org | last post: by
2 posts views Thread by puzzlecracker | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.