cfilepointerseofgetc

Is my understanding of EOF (end of file) in C correct?


Lets say we have this piece of code:

while ((ch = getc(fp)) != EOF) {
  ...
}

This is how i would assume the loop would work, please tell me if it is correct or not/if any mistakes have been made:

  1. getc reads character from I/O stream fp
  2. getc stores it in the variable ch
  3. ch is tested against EOF which is normally equivalent to -1 (depending on how it is defined for you).
  4. When EOF is returned it indicates that the End Of File indicator for a specific stream has been set. This means that a previous read operation has attempted to read past the end of the file.

One important question i have:

In this loop as getc gets to the end of the file, does it try to eventually read past the end of the file which causes it to eventually return EOF?


Solution

    1. getc reads character from I/O stream fp

    More precisely, getc attempts to read a character from the fp stream.

    1. getc stores it in the variable ch

    getc returns an int value, and the assignment ch = getc(fp) stores that value in ch, if it is representable in the ch type. If it is not representable, it is converted in a manner that depends on the ch type. To avoid complications with this conversion, ch should be int.

    1. ch is tested against EOF

    Yes, the resulting value of ch is compared with EOF. This makes the type important: If getc returns EOF and ch is, say, a character type that cannot represent the value of EOF, then the conversion will cause a different value to be stored in ch, and then it will not compare equal to EOF.

    … which is normally equivalent to -1 (depending on how it is defined for you).

    EOF may be −1 in many C implementations, but it is normal for it to have any negative int value.

    1. When EOF is returned it indicates that the End Of File indicator for a specific stream has been set.

    No, an EOF return indicates that either end-of-file has been reached (and a further read was attempted) or an error occurred. C 2018 7.21.7.5 3 says:

    The getc function returns the next character from the input stream pointed to by stream. If the stream is at end-of-file, the end-of-file indicator for the stream is set and getc returns EOF. If a read error occurs, the error indicator for the stream is set and getc returns EOF.

    This means that a previous read operation has attempted to read past the end of the file.

    It means either a previous operation or the current operation attempted to read past the end of the file or a previous operation or the current operation encountered an error (and these previous conditions have not been cleared, as by calling rewind or clearerr).

    In this loop as getc gets to the end of the file, does it try to eventually read past the end of the file which causes it to eventually return EOF?

    If the stream is connected to a file of finite and unchanging size, that loop will eventually attempt to read past the end of file, if nothing in the body of the loop resets the file position, at which point EOF will be returned and the loop will exit. However, if the stream is connected to some “infinite” supply of data, such as a pipe, it is possible the loop will never encounter the end of the stream.