this question has answer here:
- fgetc, checking eof 2 answers
fgetc
reads character file @ time, , returns character type of int
. if file ends, fgetc
returns eof
("probably" (int)(-1)
). however, have seen frequently:
char ch; while ((ch = fgetc(fp)) != eof) { /* something. */ } |<-- parn -->|
this concern:
fgetc
readsoxff
, returns0x000000ff
.0xff
assignedch
. (type castingint
char
)- if
parn
type ofchar
, promoted0xffffffff
. - break out of while loop because
parn == eof
. (stop reading file)
how can tell reading oxff
, returning eof
apart?
the whole reason return type of fgetc
int
because can return either value of unsigned char
, or eof
. documented in specification , in resource on learning c. if use object of type char
store result, programmer error.
Comments
Post a Comment