Subject: Re: main return...
To: Not for internal consumption <>
From: John F. Woods <>
List: current-users
Date: 03/27/1996 18:56:49
-   Look at all the "quite experienced" C programmers who don't know that
-	   a[i] = i++;
-   is not allowed.

> You're kidding, right?  I was sure it wasn't _recommended_, but I
> didn't know it was explicitly forbidden.

It is explicitly forbidden.  And the reason for it has nothing to do with
order of evaluation.  In between two "sequence points", if you store to an
object, you may not evaluate the object except to determine the value to be
stored in that object; you also may not store to an object more than once
between two sequence points, thus forbidding "i = ++i;".  The standard says
the behavior is undefined in both cases, meaning the compiler may emit code
which does *anything at all*, including enabling multiple functional units
in a parallel processor on a single bus simultaneously burning out the bus
driver transistors.

Even beyond that, your analysis is still flawed:

> 	[] binds first, left to right, so we have a[i].
> 	++ binds next, right to left, so i is now i+1.
> 	= binds last, right to left, so a[i] has the value i+1.

*when* do you calculate the lvalue a[i]?  Only the binding for = is specified,
not the order of evaluation.  When does the autoincrement happen?  "Sometime"
before the semicolon, that's all you know.

> (StunOS 4.x standard compiler AND gcc do this -- is this right?).

EX-CUSE me, but "it works on my compiler!" has never, EVER been the
definition of C, not since the introduction of pcc, anyway!  That's what
the standardization effort is all about!

> [Followups to that last question to be redirected to,
>  let's not start YAS semantics war.]

This isn't the first stunned response I've gotten, so I'm trying to head off
some future ones.

-   And weep for the state of education.

And to emphasize this:  "The C Programming Language", by Kernighan and Ritchie,
has been warning about THIS VERY EXAMPLE since 1978!

> I don't know a compiler that would reject the code.  Of course, that
> speaks loudly for the state of education among compiler writers, I
> suppose...

The standard permits compilers not to diagnose this, mostly because diagnosing
all possible cases of violation of this requirement would be impossible.  While
a compiler could pick up really obvious cases, presumably most compiler writers
regard their time better spent adding Dhrystones than diagnostics...