The C specification states that an int should be able to hold values from -32767 to 32767 at a minimum. Any platform with a smaller int non-standard.
The C specification also states that EOF is a negative constant of int and that fgetc returns "a unsigned char converted to int " if read successfully. Since unsigned char cannot have a negative value, the EOF value can be distinguished from any read from the stream. *
* See below a case of loopholes in which this fails.
Corresponding standard text (from C99):
If UCHAR_MAX ≤ INT_MAX , there is no problem: all unsigned char values will be converted to non-negative integers, so they will be different from EOF.
Now there is a funny loophole: if the system has UCHAR_MAX > INT_MAX , then it is legally allowed to convert values greater than INT_MAX into negative integers (in accordance with § 6.3.3.3, the result of converting a value to a signed type that cannot represent this value is determined implementation), allowing you to convert a character read from a stream to EOF.
There are systems with CHAR_BIT > 8 (e.g. TI C4x DSP, which apparently uses 32-bit bytes), although I'm not sure if they are broken relative to the EOF and stream functions.
nneonneo
source share