fgetc
reads a character from a file at a time, and returns the character as type of int
. If the file ends, then fgetc
returns EOF
("probably" (int)(-1)
). However, I have seen this so frequently:
char ch;
while ((ch = fgetc(fp)) != EOF) { /* Do something. */ }
|<-- PARN -->|
This is what I concern:
fgetc
reads OxFF
and returns 0x000000FF
.0xFF
is assigned to ch
. (type casting from int
to char
)PARN
is type of char
, then it will be promoted to 0xFFFFFFFF
.PARN == EOF
. (stop reading from the file)How can we tell reading OxFF
and returning EOF
apart?
The whole reason the return type of fgetc
is int
is because it can return either a value of an unsigned char
, or EOF
. This is clearly documented in the specification and in any good resource on learning C. If you use an object of type char
to store the result, this is programmer error.