Representing EOF in C code?

2020-01-27 15:19发布

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

10条回答
smile是对你的礼貌
2楼-- · 2020-01-27 16:12

The answer is NO, but...

You may confused because of the behavior of fgets()

From http://www.cplusplus.com/reference/cstdio/fgets/ :

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

查看更多
聊天终结者
3楼-- · 2020-01-27 16:15

The EOF character recognized by the command interpreter on Windows (and MSDOS, and CP/M) is 0x1a (decimal 26, aka Ctrl+Z aka SUB)

It can still be be used today for example to mark the end of a human-readable header in a binary file: if the file begins with "Some description\x1a" the user can dump the file content to the console using the TYPE command and the dump will stop at the EOF character, i.e. print Some description and stop, instead of continuing with the garbage that follows.

查看更多
家丑人穷心不美
4楼-- · 2020-01-27 16:17

This is system dependent but often -1. See here

查看更多
时光不老,我们不散
5楼-- · 2020-01-27 16:17

The value of EOF can't be confused with any real character.

If a= getchar(), then we must declare a big enough to hold any value that getchar() returns. We can't use char since a must be big enough to hold EOF in addition to characters.

查看更多
登录 后发表回答