Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!mtune!codas!cpsc6a!rtech!wrs!dg From: dg@wrs.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <222@wrs.UUCP> Date: Mon, 6-Jul-87 21:42:55 EDT Article-I.D.: wrs.222 Posted: Mon Jul 6 21:42:55 1987 Date-Received: Sat, 11-Jul-87 00:36:51 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <22635@sun.uucp> <262@auvax.UUCP> Reply-To: dg@wrs.UUCP (David Goodenough) Organization: Wind River Systems, Emeryville, CA Lines: 71 In article <262@auvax.UUCP> rwa@auvax.UUCP (Ross Alexander) writes: >The following discussion is all relative to > > #define EOF -1 > char c; > while ( ( c = getchar() ) != EOF ) { /* do something */ } In the above case I have worked this on several machines (PDP 11/44A, VAX 11/750, 68K, Z80) and in the first three cases I get the following _WARNING_: "Non-portable character comparison" However on the Z80 it gets it wrong (because chars are implicitly unsigned) ..... >I argue >that this is because in this context, the assignment is a side >effect, and isn't intended to `narrow' the value of the expression. >Just to point out where the logical extension of case two gets us, >what is the value of x in this fragment: > > float x; > int i; > > x = 2.0 + ( i = 3.5 ); > >I would say 5.5; others might say 5.0, it seems. But if I _wanted_ >5.0, I would expect to write > > x = 2.0 + (int) ( i = 3.5 ); > >and I appeal to the principle of least astonishment for justification. /* FLAME THROWER OUT!! :-) */ Now hold on just a minute !!!!! - aren't we forgetting something about the order of evaluation rules here: the (i = 3.5) _MUST_ be evaluated first, and evaluates to an _int_ type expression whose value is 3 (i.e. the value assigned into i, so we effectively have: x = 2.0 + 3 and the day that evaluates to 5.5 I'm gonna jump off the Golden Gate: Think about the following: float x; float y; int i; int j; j = (x = 2.0 + (i = 3.5)); (y = 2.0 + (i = 3.5)); Are you going to try to convince me that the two lines above put different values into x and y??????, because that seems to be what is being said here: type coercion works from the INSIDE OUT, NOT the OUTSIDE IN, i.e. let each expression look after itself; i.e. you can't cast the (i = 3.5) based on the fact that it's about to be assigned into a float, it is an integer type expression pure and simple. /* FLAME THROWER AWAY!! :-) */ -- dg@wrs.UUCP - David Goodenough +---+ | +-+-+ +-+-+ | +---+