### Question 12.1

What's wrong with this code?

    char c;
    while((c = getchar()) != EOF) ...

For one thing, the variable to hold `getchar`'s return value must be an `int`. `getchar` can return all possible character values, as well as `EOF`. By passing getchar's return value through a `char`, either a normal character might be misinterpreted as `EOF`, or the `EOF` might be altered (particularly if type `char` is unsigned) and so never seen.

References: K&R1 Sec. 1.5 p. 14
K&R2 Sec. 1.5.1 p. 16
ANSI Sec. 3.1.2.5, Sec. 4.9.1, Sec. 4.9.7.5
ISO Sec. 6.1.2.5, Sec. 7.9.1, Sec. 7.9.7.5
H&S Sec. 5.1.3 p. 116, Sec. 15.1, Sec. 15.6
CT&P Sec. 5.1 p. 70
PCS Sec. 11 p. 157

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback