Can u anybody of u explain me how the following statement works??
y=x++<=5;:!:

Dani AI

Generated

Short answer: the posted expression uses the old value of the variable for the comparison, the comparison yields 1 or 0 which is stored in the left-hand variable, and the increment side effect is applied sometime before the end of the full expression (the semicolon) — not necessarily before the assignment. This is the point , and were circling around.

Why: the postfix ++ is part of the operand given to the relational operator, so parsing and precedence put the ++ together with the variable before the <= is applied. The postfix operator produces the original value for the comparison; its side effect (actually changing the variable) is deferred until later in the full-expression evaluation. The language guarantees the increment will have happened by the time control reaches the next statement, but it does not guarantee the increment happens before the assignment that stores the comparison result.

Concrete outcome (numbers make it clearer): if the variable starts at 5, the comparison sees 5 and the boolean result stored is 1; after the statement completes the variable will be 6. Whether the increment physically happened before or after the assignment is unspecified, but the final values are well defined in this simple case.

Caution: mixing reads and writes to the same scalar more than once in one expression can be undefined (for example i = i++; is not legal). For a reliable, readable program, keep side effects on their own lines or separate the operations so intent and ordering are explicit. Further formal details on evaluation order and sequencing are documented in the C language reference (see C evaluation order).

Recommended Answers

All 13 Replies

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.

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?

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.

So the error was this statement? y=x++<=5; is equivalent to y=(x++<=5); .

No.

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.

I know that it is done after the comparison. I wanted to know if it was done after or before the assignment.

My knowledge suggest that asignment is done before the incrementation.

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.

Grr.

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?

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. :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.