[net.lang.c] What should a C compiler do?

keashly@mprvaxa.UUCP (03/24/84)

While playing with pcc I stumbled on the following bits
of C that I think are handled incorrectly. The following
code contains two examples:

m(){
int a,b,c;
float d;
   /* case 1 a float switch which the compiler should flag
      as an error (K&R 202) but  pcc (4.1 BSD) doesn't and it
      spits out incorrect code. */
   switch (d) {
      case 4:
	  break;
      case 3:
	  break;
      }

   /* case 2 ideally "a" should have the same value after but pcc
      interprets them differently (K&R say that it is OK 185) */
   if ( a = b++ + b );
   a = b++ + b;
}


All I'm after are comments on the two cases. Myself, I believe 1 is wrong
and 2 disturbing. Thanks.
-- 

    Lance Keashly
    Microtel Pacific Research
    ..decvax!microsoft!ubc-vision!mprvaxa!keashly

    8999 Nelson Way
    Burnaby, B.C., Canada,
    V5A 4B5

darryl@ism780.UUCP (03/29/84)

#R:mprvaxa:-50100:ism780:12500001:000:887
ism780!darryl    Mar 27 16:03:00 1984

Very clearly #1 is wrong.  K&R says "The usual arithmetic
conversion is peformed on the expression, but the result must
be int."

I don't find anything disturbing about #2 at all.  The result of
b++ is the original value of b.  After it has been used in
computing the expression, it is incremented.  Several places in
K&R mention that the only way to specify evaluation order in an
expression is to write several expressions, assigning results
to temps.

In fact, the reason that you get different code is easily
explained:  normally the actual increment is put off until after
evaluation of the expression is complete -- this is what you get
for "a = b++ + b;".  But since the result of the expression
"if( a = b++ + b ) ;" might result in a branch before the end of
the statement, the increment is done right after b is fetched, the
first time.

	Darryl Richman  ...!ima!ism780!darryl