[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] OT Generic C question

From: Dave Hansen
Subject: Re: [avr-gcc-list] OT Generic C question
Date: Thu, 22 Sep 2005 15:11:53 -0400

From: Richard Urwin <address@hidden>

On Tuesday 20 Sep 2005 15:04, Dave Hansen wrote:

[...re: i = i++...]

> The standard says that the value of a variable should only be
> modified once between sequence points.  The expression "i = i++;"
> attempts to modify i twice: once with the ++ operator and once with
> the = operator.  Modifying a variable twice between sequence points
> is undefined behavior -- there is no "correct" answer.  After the
> expression is executed, i could be 0, 1, 42, or the system might
> format your hard drive.  All of the above and many other results are
> "legal" as far as the standard is concerned.

No, you're wrong.
There is a sequence point immediately before the assignment.
So, far from being a compiler bug, this is absolutely correct behaviour.
Anything else would be a bug.

Chapter and verse, please.

Never mind.  I'll save you the trouble.  6.5p2 says (in part)

  "Between the previous and next sequence point an
   object shall have its stored value modified at most
   once by the evaluation of an expression."

But you're not disputing that. tells us there is a sequence point immediately after a function's parameters have been evalutated and the function itself is called, but that doesn't apply here. 6.5.13p4 gives us one for the LHS of the && operator, 6.5.14p4 likewise tells of one before the || operator, and 6.5.15p4 says there's one after the ? in the ternary conditional operator, but none of those apply either.

6.5.16p4 explicitly says of all the assignment operators (=, *=, /=, %=, +=, -=, <<=, >>=, &=, ˆ=, |=) that the order of the evaluation of the operands is unspecified, and that the result of an assignment operator cannot be modified. But it gives no hint of any sequence point before the assignment takes place.

6.5.17p2 lets us know the comma operator introduces another sequence point. 6.7.5p3 says the end of a full declarator is a sequence point. 6.8p4 says the end of a full expression is a sequence point 7.1.4p3 says there's another one just before a library function returns, and 7.19.6p1 and 7.24.2p1 says there's one after the action of each format specifier in *printf and *scanf. 7.20.5p5 says another occurs immediately before and after each call to the comparison function for bsearch and qsort.

Notice that not even parentheses introduce sequence points.

But, if you don't believe me, go ahead and suggest there's a sequence point before the assignment in i = i++ on comp.lang.c. Or see http://www.eskimo.com/~scs/C-faq/q3.8.html and http://www.eskimo.com/~scs/C-faq/q3.9.html to save yourself from grief.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]