Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!topaz.rutgers.edu!ron From: ron@topaz.rutgers.edu (Ron Natalie) Newsgroups: comp.lang.c Subject: Re: Writing readable code Message-ID: <13112@topaz.rutgers.edu> Date: Thu, 2-Jul-87 13:25:54 EDT Article-I.D.: topaz.13112 Posted: Thu Jul 2 13:25:54 1987 Date-Received: Sat, 4-Jul-87 06:35:29 EDT References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> Organization: Rutgers Univ., New Brunswick, N.J. Lines: 25 > 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 indication. This is a common error in C, but it is fortunate that most text this is used on never contains any characters with the most significant bit set. 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; -Ron