Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site rabbit.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!rabbit!ark From: ark@rabbit.UUCP (Andrew Koenig) Newsgroups: net.unix-wizards Subject: Re: getc() != EOF Message-ID: <2836@rabbit.UUCP> Date: Wed, 30-May-84 14:39:46 EDT Article-I.D.: rabbit.2836 Posted: Wed May 30 14:39:46 1984 Date-Received: Fri, 1-Jun-84 22:24:08 EDT References: <307@basser.SUN> Organization: AT&T Bell Laboratories, Murray Hill Lines: 43 >>> >>> In all conscience, >>> >>> while ( (c = getc()) != EOF ) >>> >>> ought to work. If somebody is to be blamed, it is surely not the >>> people who wrote the code, but the people who made a C implementation >>> that broke it. >> >> It will work if "c" is declared "int." >> It will not work if "c" is declared "char." >> >> Variable declarations are an essential part of the program, and should be >> included in illustrative code fragments, so problems are not concealed. >> >> Ed Nather > WRONG! The code above will work if c is int or char. > Char variables are promoted to int in expressions (see C manual) > and a char -1 is IDENTICAL with an int -1. Unsigned char c could be > different (Any C implementors there? (kvm?)). > > Chris Maltby > University of Sydney > Ed Nather is right here: a char -1 is not identical to an int -1. C isn't obligated to sign-extend characters when converting to ints, although it is obligated to refrain from sign-extending unsigned chars. Getc (and getchar) return ints, not chars, and the result returned is always non-negative (except EOF), even on those machines that sign-extend characters. If I write: char c; while ((c = getc (file)) != EOF) ... I will lose on a machine that sign-extends chars as soon as I read a char with all its bits turned on, but it will work OK if c is an int. --Andrew Koenig