Content deleted Content added
Quuxplusone (talk | contribs) →Reading from a stream using fgetc: merge from article "getchar" |
Quuxplusone (talk | contribs) m →Reading from a stream using fgetc: '''EOF''' -> EOF |
||
Line 80:
When <code>getchar</code>'s result is assigned to a <code>char</code>, which can represent only 256 different values, there is necessarily some loss of information — when packing 257 items into 256 slots, there [[Pigeonhole principle|must be a collision]]. The <code>EOF</code> value, when converted to <code>char</code>, becomes indistinguishable from whichever one of the 256 characters shares its numerical value. If that character is found in the file, the above example may mistake it for an end-of-file indicator; or, just as bad, if type <code>char</code> is unsigned, then because <code>EOF</code> is negative, it can never be equal to any unsigned <code>char</code>, so the above example will not terminate at end-of-file. It will loop forever, repeatedly printing the character which results from converting <code>EOF</code> to <code>char</code>.
On systems where <code>int</code> and <code>char</code> are the same size, even the "good" example will suffer from the indistinguishability of
===External link===
|