Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!hao!oddjob!gargoyle!ihnp4!homxb!ho95e!wcs From: wcs@ho95e.ATT.COM (Bill.Stewart) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <1519@ho95e.ATT.COM> Date: Tue, 7-Jul-87 18:13:54 EDT Article-I.D.: ho95e.1519 Posted: Tue Jul 7 18:13:54 1987 Date-Received: Sat, 11-Jul-87 00:55:49 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <22635@sun.uucp> <262@auvax.UUCP> Reply-To: wcs@ho95e.UUCP (46133-Bill.Stewart,2G218,x0705,) Organization: AT&T Bell Labs 46133, Holmdel, NJ Lines: 39 Keywords: (un)signed characters 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 article <22635@sun.uucp>, guy%gorodish@Sun.COM (Guy Harris) writes: : > > The assignment into a character causes getchar's int return : > > to be changed to char, invalidating the following comparison> : > On a machine where "char" is not signed, this won't work even if you : > run it on text consisting entirely of 7-bit characters. "getchar" : > will return EOF when it hits end-of-file; EOF (-1) will get converted : > to '\377' ... which compares equal to 255 but not to -1. : : Isn't there a little room here for arguement? Now of course K&R say : that an expression is coerced to the type it is being assigned to. As with other expressions, the definition of the language specifies the value and type of the expression. For an assignment expression "lvalue = rvalue", the type is the type of the lvalue, and the value is the value assigned to the lvalue. : But isn't assignment, in the strictest sense, a side effect? Yes, in some sense. Consider ++n and n++. Both have the side effect of incrementing n, but they have different values. : and I appeal to the principle of least astonishment for justification. Consider the case char c,d; c = -1; printf( "%d %d\n", c, (d = -1) ); By "least astonishment", it should print the same number twice. On your VAX, that will be -1 -1; on my 3B it will be 255 255, because characters have values in the range 0 - 255, not -128 - 127. While the 255 is mildly surprising, it's a lot less surprising than 255 -1 would be. But yes, people occasionally get surprised when they move code from VAXen to other machines. -- # Bill Stewart, AT&T Bell Labs 2G-202, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs