[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: i = i++



Babu Kalakrishnan forced the electrons to say:
> A few days back exactly the same discussion came up in the 
> comp.lang.java.programmer newsgroup and there were some interesting comments
> about how Java & C treat such constructs.

This topic in fact comes up every so often on comp.lang.c. It is a FAQ
also, found at http://www.eskimo.com/~scs/C-faq/top.html.

> 2) In C & C++ the result is undefined which means results can vary depending
>    on compiler implementations.

Actually, there is a distinction between undefined and implementation
defined. If I have understood it correctly, the difference is that
implementation defined means that the implementation has to define
it in a consistent way, and has to document it, while undefined means
that implementations can do whatever they feel like (even change their
behaviour across compiler runs), without even documenting.

Coming back to the original question (even though it was about C++, I am
talking of C here), the C standard says, in section 6.5,

       [#2] Between the previous and next sequence point an  object
       shall  have  its  stored  value modified at most once by the
       evaluation of an expression.  Furthermore, the  prior  value
       shall  be  accessed  only  to  determine  the  value  to  be
       stored.60)                                                   *

And the footnote 60 is:

       60)This paragraph renders  undefined  statement  expressions
          such as
                  i = ++i + 1;
                  a[i++] = i;
          while allowing
                  i = i + 1;
                  a[i] = i;

The standard is quite clear here that the original expression (i = i++;)
is undefined.

Binand