Need a little guidance??
Can u anybody of u explain me how the following statement works??
y=x++<=5;:!:
himanjim
Junior Poster in Training
67 posts since Jul 2006
Reputation Points: 14
Solved Threads: 1
If x is less than or equal to 5, y is set to 1 -- otherwise y is set to 0; x is then incremented.
But better would be for you to ask, "I think that this statement does [...]. Is this correct?" Or at least provide a (minimal, but complete and compileable) snippet that shows the full context.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
If x is less than or equal to 5, y is set to 1 -- otherwise y is set to 0; x is then incremented.
Is x incremented before or after the result of the comparison is assigned to y? Correct me if I am wrong in the explaination below. y=x++<=5; is equivalent to y=(x++<=5); .
The part inside the brackets should be evaluated first. So doesn't that mean the binary comparison and the ++ should also be evaluated before the assignment?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Is x incremented before or after the result of the comparison is assigned to y?
After.Correct me if I am wrong in the explaination below.
y=x++<=5; is equivalent to y=(x++<=5); .
The part inside the brackets should be evaluated first. So doesn't that mean the binary comparison and the ++ should also be evaluated before the assignment?No.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
So the error was this statement?
y=x++<=5; is equivalent to y=(x++<=5); .
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
So the error was this statement?
y=x++<=5; is equivalent to y=(x++<=5); .
This is the case of post incrementation so increment is done after the comparision.
himanjim
Junior Poster in Training
67 posts since Jul 2006
Reputation Points: 14
Solved Threads: 1
I know that it is done after the comparison. I wanted to know if it was done after or before the assignment.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
My knowledge suggest that asignment is done before the incrementation.
himanjim
Junior Poster in Training
67 posts since Jul 2006
Reputation Points: 14
Solved Threads: 1
I know that it is done after the comparison. I wanted to know if it was done after or before the assignment.
I'll go out on a limb a little (and search more later) and say that it is done after the comparison and before the assignment.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Now just to be an ass I'll change my mind and reverse that last statement of mine.
No, not just to be an ass:
http://c-faq.com/expr/seqpoints.html
My question is why? Why is it a curiousity? Why not just write it without ambiguity?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Now just to be an ass I'll change my mind and reverse that last statement of mine.
No, not just to be an ass:
http://c-faq.com/expr/seqpoints.html
Snarl. My question is why? Why is it a curiousity? Why not just write it without ambiguity?When I write code I try to be clear and without ambiguity. But I can't give a answer like that when a newbie asks something about operator call sequence. So I have to be careful on subtle points like that (especially since I am a mod). I will go through the link and see what I can make out from it. :)
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
I will go through the link and see what I can make out from it. :)
What I got from it was this:The sequence points listed in the C standard are:at the end of the evaluation of a full expression (a full expression is an expression statement, or any other expression which is not a subexpression within any larger expression);
at the ||, &&, ?:, and comma operators; and
at a function call (after the evaluation of all the arguments, and just before the actual call).
Conspicuously missing was the assignment operator.
I still find sequence points confusing, so I do my best simply to avoid writing code that makes great use of knowing exactly where they are.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314