Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!greg From: greg@utcsri.UUCP Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <5065@utcsri.UUCP> Date: Sun, 12-Jul-87 16:43:44 EDT Article-I.D.: utcsri.5065 Posted: Sun Jul 12 16:43:44 1987 Date-Received: Sun, 12-Jul-87 18:35:16 EDT References: <7220@mimsy.UUCP> <47000012@uiucuxe> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 40 Summary: In article <47000012@uiucuxe> mcdonald@uiucuxe.cso.uiuc.edu writes: >>> The main advantage of this idiom is for "while" statements. The usual >>> example is "while ((c = getchar()) != EOF) ...", which cannot be >>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(), i != EOF){ >> 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 ) ? > Both are correct, (a,b,c) is equivalent to ((a,b),c). But WHY DO YOU NEED char c IN THE FIRST PLACE??? There is no reason to use char instead of int, except to save space in arrays or structs. Simple auto variables should be declared as int. Using a char instead of int in this case will rarely save space, and will often produce slower code (grotty 8088 machines notwithstanding). Not to mention all the EOF pitfalls we've been talking about (oops I mentioned it). The mnemonic effect of making the variable a 'char' because you are using it to store a character is not really worth the trouble. If the type had been called 'byte' we would be using it less often and having fewer problems. >As a poor C novice I see no way around this short of putting c = i >on a separate line. And there is nothing wrong with that, is there? -- ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg Have vAX, will hack...