C语言中的EOF是什么?
如何查看最后打印的内容?换句话说,EOF 应该填什么?我检查了定义,它说 EOF 是 -1。
如果输入 Ctrl-D,您将看不到任何内容。
#include <stdio.h>
int main() {
int c;
while((c = getchar() != EOF)) {
printf("%d\n", c);
}
printf("%d - at EOF\n", c);
}
How do you get to see the last print? In other words what to put in for EOF? I checked the definitions and it says EOF is -1.
And if you enter Ctrl-D you won't see anything.
#include <stdio.h>
int main() {
int c;
while((c = getchar() != EOF)) {
printf("%d\n", c);
}
printf("%d - at EOF\n", c);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在 Linux 系统和 OS X 上,输入导致 EOF 的字符是 Ctrl-D。对于 Windows,它是 Ctrl-Z。
根据操作系统的不同,该字符仅当它是一行中的第一个字符(即 Enter 后的第一个字符)时才起作用。由于控制台输入通常是面向行的,因此系统也可能无法识别 EOF 字符,直到您按 Enter 进行跟踪。
是的,如果该字符被识别为 EOF,那么您的程序将永远不会看到实际的字符。相反,C 程序将从
getchar()
获取-1
。On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.
Depending on the operating system, this character will only work if it's the first character on a line, i.e. the first character after an Enter. Since console input is often line-oriented, the system may also not recognize the EOF character until after you've followed it up with an Enter.
And yes, if that character is recognized as an EOF, then your program will never see the actual character. Instead, a C program will get a
-1
fromgetchar()
.您应该将括号更改为
因为“=”运算符的优先级低于“!=”运算符。然后你就会得到预期的结果。您的表达式等于
您将得到两个 1 作为输出,因为您正在进行比较“c!= EOF”。这将始终成为您输入的字符的一个,然后按回车键后成为“\n”。除了最后一个比较(其中 c 实际上是 EOF)之外,它会给您一个 0。
关于 EOF 的编辑:EOF 通常为 -1,但这不受标准保证。该标准仅在第7.19.1节中定义了有关EOF的内容:
假设 EOF 等于 -1 是合理的,但使用 EOF 时,不应针对特定值进行测试,而应使用宏。
You should change your parenthesis to
Because the "=" operator has a lower precedence than the "!=" operator. Then you will get the expected results. Your expression is equal to
You are getting the two 1's as output, because you are making the comparison "c!=EOF". This will always become one for the character you entered and then the "\n" that follows by hitting return. Except for the last comparison where c really is EOF it will give you a 0.
EDIT about EOF: EOF is typically -1, but this is not guaranteed by the standard. The standard only defines about EOF in section 7.19.1:
It is reasonable to assume that EOF equals -1, but when using EOF you should not test against the specific value, but rather use the macro.
EOF 的值是一个负整数,以区别于 0 到 255 范围内的“char”值。它通常为 -1,但它可以是任何其他负数...根据 POSIX 规范,因此您不应该假设它是-1。
^D 字符是您在 UNIX/Linux 上的控制台流中键入的字符,用于告诉它逻辑上结束输入流。但在其他情况下(例如当您从文件中读取时),它只是另一个数据字符。不管怎样,^D 字符(表示输入结束)永远不会出现在应用程序代码中。
正如 @Bastien 所说,如果 getchar() 失败,也会返回 EOF。严格来说,您应该调用
ferror
或feof
来查看 EOF 是否表示错误或流结束。但在大多数情况下,您的应用程序在任何一种情况下都会执行相同的操作。The value of EOF is a negative integer to distinguish it from "char" values that are in the range 0 to 255. It is typically -1, but it could be any other negative number ... according to the POSIX specs, so you should not assume it is -1.
The ^D character is what you type at a console stream on UNIX/Linux to tell it to logically end an input stream. But in other contexts (like when you are reading from a file) it is just another data character. Either way, the ^D character (meaning end of input) never makes it to application code.
As @Bastien says, EOF is also returned if
getchar()
fails. Strictly speaking, you should callferror
orfeof
to see whether the EOF represents an error or an end of stream. But in most cases your application will do the same thing in either case.EOF 表示文件结束。这是到达文件末尾的标志,并且将不再有数据。
编辑:
我纠正了。在这种情况下,它不是文件结尾。如前所述,当传递 CTRL+d (linux) 或 CTRL+z (windows) 时,它也会被传递。
EOF means end of file. It's a sign that the end of a file is reached, and that there will be no data anymore.
Edit:
I stand corrected. In this case it's not an end of file. As mentioned, it is passed when CTRL+d (linux) or CTRL+z (windows) is passed.
几个拼写错误:
代替:
getchar() 将返回键视为有效输入,因此您也需要对其进行缓冲。EOF 是指示输入结束的标记。通常它是一个所有位都已设置的 int。
打印:
用于输入:
Couple of typos:
in place of:
Also getchar() treats a return key as a valid input, so you need to buffer it too.EOF is a marker to indicate end of input. Generally it is an int with all bits set.
prints:
for input:
来自终端的输入永远不会真正“结束”(除非设备已断开连接),但在终端中输入多个“文件”很有用,因此保留了一个按键序列来指示输入结束。在 UNIX 中,击键到 EOF 的转换是由终端驱动程序执行的,因此程序不需要区分终端和其他输入文件。默认情况下,驱动程序将行开头的 Control-D 字符转换为文件结束指示符。要将实际的 Control-D (ASCII 04) 字符插入到输入流中,用户需要在其前面加上“quote”命令字符(通常是 Control-V)。 AmigaDOS 类似,但使用 Control-\ 而不是 Control-D。
在 Microsoft 的 DOS 和 Windows(以及 CP/M 和许多 DEC 操作系统)中,从终端读取永远不会产生 EOF。相反,程序识别源是终端(或其他“字符设备”)并将给定的保留字符或序列解释为文件结束指示符;最常见的是 ASCII Control-Z,代码 26。某些 MS-DOS 程序,包括 Microsoft MS-DOS shell (COMMAND.COM) 的一部分和操作系统实用程序(例如 EDLIN),将 Control-Z 视为在文本文件中作为标记有意义数据的结尾,和/或在写入文本文件时将 Control-Z 添加到末尾。这样做有两个原因:
nput from a terminal never really "ends" (unless the device is disconnected), but it is useful to enter more than one "file" into a terminal, so a key sequence is reserved to indicate end of input. In UNIX the translation of the keystroke to EOF is performed by the terminal driver, so a program does not need to distinguish terminals from other input files. By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. To insert an actual Control-D (ASCII 04) character into the input stream, the user precedes it with a "quote" command character (usually Control-V). AmigaDOS is similar but uses Control-\ instead of Control-D.
In Microsoft's DOS and Windows (and in CP/M and many DEC operating systems), reading from the terminal will never produce an EOF. Instead, programs recognize that the source is a terminal (or other "character device") and interpret a given reserved character or sequence as an end-of-file indicator; most commonly this is an ASCII Control-Z, code 26. Some MS-DOS programs, including parts of the Microsoft MS-DOS shell (COMMAND.COM) and operating-system utility programs (such as EDLIN), treat a Control-Z in a text file as marking the end of meaningful data, and/or append a Control-Z to the end when writing a text file. This was done for two reasons:
我认为这是检查 EOF 值的正确方法。
我检查了输出。
对于输入:abc 和 Enter,我得到输出:97 98 99 10。(ASCII 值)
对于输入 Ctrl-D,我得到输出:-1 - 在 EOF 处。
所以我认为-1是EOF的值。
尝试使用其他输入代替 Ctrl-D,例如 Ctrl-Z。
我认为它因编译器而异。
I think this is right way to check value of EOF.
And I checked the output.
For INPUT: abc and Enter I got OUTPUT: 97 98 99 10. ( the ASCII values)
For INPUT Ctrl-D I got OUTPUT: -1 - at EOF.
So I think -1 is the value for EOF.
Try other inputs instead of Ctrl-D, like Ctrl-Z.
I think it varies from compiler to compiler.
为了简单起见:EOF 是一个值为 -1 的整数类型。因此,我们必须使用整型变量来测试EOF。
to keep it simple: EOF is an integer type with value -1. Therefore, we must use an integer variable to test EOF.
修改上面的代码以使 EOF 更加清晰,按 Ctrl+d 并使用 putchar 来打印字符,避免在 while 循环中使用 printf。
modified the above code to give more clarity on EOF, Press Ctrl+d and putchar is used to print the char avoid using printf within while loop.