c - What will happen if fgetc reads 0xFF? -


this question has answer here:

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:

  1. fgetc reads oxff , returns 0x000000ff.
  2. 0xff assigned ch. (type casting int char)
  3. if parn type of char, promoted 0xffffffff.
  4. 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