*BSD News Article 84098


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!nntp.coast.net!newsfeed.dacom.co.kr!usenet.kornet.nm.kr!agate!spool.mu.edu!uwm.edu!alpha1.csd.uwm.edu!bacon
From: bacon@alpha1.csd.uwm.edu (Jason Wayne Bacon)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Compiler bug?
Date: 3 Dec 1996 00:45:57 GMT
Organization: University of Wisconsin - Milwaukee, Computing Services Division
Lines: 50
Message-ID: <57vt85$top@uwm.edu>
References: <55tf99$6b5@garuda.synet.net> <57hsae$cpm@uwm.edu> <57l5t1$hr0@uriah.heep.sax.de>
NNTP-Posting-Host: 129.89.169.1

In article <57l5t1$hr0@uriah.heep.sax.de> joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) writes:
>bacon@alpha1.csd.uwm.edu (Jason Wayne Bacon) wrote:
>
>> >  >>         a=c[b++]|((c[b++])<<8);
>
>> The behaviour of this statement *is* well defined.
>
>No, it isn't.
>
>> The | operator evaluates left to right, so the operations
>> *should* occur in the followign order:
>
>No.  You're confusing expression evaluation with associativity.

OK, you got me.  I confused order of evaluation with associativity.
Since the two ++ operators aren't bound to the same operand
(as in a-b-c) associativity doesn't apply.
         ^
BTW, I also meant to say the ++ above, not |.  Rough day.  :-)

>There are only two exceptions in C where the standard requires a
>particular sequence of evaluation: the comma operator (obvious), and
>the && and || operators (so you can say ``if (ptr && *ptr ...)'').
>For everything else, only the associativity is defined.

Yeah, and it's too bad.  The C standard probably should
define the order of evaluation in these cases to avoid ambiguity.

To set everything straight, I came up with a simple example to 
prove myself wrong in my original statement:

	int 	c = 2;

	printf("%d\n",c++ * --c);

This will produce 1 if --c is evaluated first, or 4 if c++
is done first.  I actually got both possible results using
various different compilers.
The only time you have to worry is when using
++, --, or any of the various assignment operators, which alter
a variable before the expression is completely evaluated.  In other
cases, it doesn't matter which operand is evaluated first.

Ex.  (a+b) * (c+d)

Whether you evaluate (a+b) or (c+d) first, you get the same result.

Sorry if I caused any confusion,

-Jason