Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!bloom-beacon!mit-eddie!ll-xn!ames!ptsfa!ihnp4!inuxc!iuvax!pur-ee!uiucdcs!uxc.cso.uiuc.edu!uiucuxe!mcdonald From: mcdonald@uiucuxe.cso.uiuc.edu Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <47000012@uiucuxe> Date: Sun, 5-Jul-87 11:25:00 EDT Article-I.D.: uiucuxe.47000012 Posted: Sun Jul 5 11:25:00 1987 Date-Received: Tue, 7-Jul-87 06:49:57 EDT References: <7220@mimsy.UUCP> Lines: 35 Nf-ID: #R:mimsy.UUCP:7220:uiucuxe:47000012:000:1153 Nf-From: uiucuxe.cso.uiuc.edu!mcdonald Jul 5 10:25:00 1987 >/* Written 12:25 pm Jul 2, 1987 by ron@topaz.rutgers.edu in uiucuxe:comp.lang.c */ > >> The main advantage of this idiom is for "while" statements. The usual >> example is "while ((c = getchar()) != EOF) ...", which cannot be >> written cleanly without the embedded assignment. The use in "if" >> statements often permits one to collapse nested ifs, which can >> *improve* code readability. > >If "c" is of type "char" this is still not written cleanly. EOF is >type int. The assignment into a character causes getchar's int return >to be changed to char, invalidating the following comparison. The >effect is that characters of value 0xFF will cause erroneous end of >file ... >To do this right you need an extra int temporary value > > while ((i = getchar()) != EOF) > or > while( i = getchar(), i != EOF) > >followed by > c = i; How about using the notorious comma operator TWICE: while(i = getchar(), c = i , i != EOF) Is this correct, or would you need while( (i = getchar(), c = i ), i != EOF ) ? As a poor C novice I see no way around this short of putting c = i on a separate line. Doug McDonald