Representing EOF in C code?

后端 未结 10 579
夕颜
夕颜 2020-11-29 00:24

The newline character is represented by \"\\n\" in C code. Is there an equivalent for the end-of-file (EOF) character?

10条回答
  •  眼角桃花
    2020-11-29 00:56

    No. EOF is not a character, but a state of the filehandle.

    While there are there are control characters in the ASCII charset that represents the end of the data, these are not used to signal the end of files in general. For example EOT (^D) which in some cases almost signals the same.

    When the standard C library uses signed integer to return characters and uses -1 for end of file, this is actually just the signal to indicate than an error happened. I don't have the C standard available, but to quote SUSv3:

    If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgetc() shall return EOF. If a read error occurs, the error indicator for the stream shall be set, fgetc() shall return EOF, and shall set errno to indicate the error.

提交回复
热议问题