Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!husc6!think!ames!ucbcad!ucbvax!decvax!ima!haddock!karl
From: karl@haddock.ISC.COM (Karl Heuer)
Newsgroups: comp.lang.c
Subject: Re: Writing readable code
Message-ID: <730@haddock.ISC.COM>
Date: Tue, 14-Jul-87 18:03:54 EDT
Article-I.D.: haddock.730
Posted: Tue Jul 14 18:03:54 1987
Date-Received: Fri, 17-Jul-87 00:49:16 EDT
References: <7220@mimsy.UUCP> <47000012@uiucuxe> <5065@utcsri.UUCP>
Reply-To: karl@haddock.ISC.COM (Karl Heuer)
Organization: Interactive Systems, Boston
Lines: 30

In article <5065@utcsri.UUCP> greg@utcsri.UUCP (Gregory Smith) writes:
>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 ...
>>>To do this right you need an extra int temporary value.
>But WHY DO YOU NEED char c IN THE FIRST PLACE???

As the person who posted the original idiom, I'll mention here that I intended
the variable c to be type int; I left out the explicit declaration and the
#include  because I assumed both were well-known.  If I had known the
subject was going to change like this, I would have included a footnote.

>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.

Not entirely true; consider "while ((c=getchar()) != EOF) write(1, &c, 1);".
This is incorrect% for "int" as well as for "char", and the simplest way to
make it work is to use one of each: "char c; int i; while ((i=getchar()) != \
EOF) { c=i; write(1, &c, 1); }".

>The mnemonic effect of making the variable a 'char' because you are using it
>to store a character is not really worth the trouble.

Given the semantics of getchar(), I agree.  However, I consider getchar() to
be a botch; given proper error%% handling, getchar() *should* return a char.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
% With "int c" it happens to work on a VAX.  That doesn't make it right.
%% This includes end-of-file, although it isn't an "error" in the usual sense.