ctrl-d 没有停止 while(getchar()!=EOF) 循环[重复]

2024-02-08

Here is my code. I run it in ubuntu with terminal. when I type (a CtrlD) in terminal, the program didn't stop but continued to wait for my input.

Isn't CtrlD equal to EOF in unix?

谢谢。

#include<stdio.h>

main() {
    int d;
    while(d=getchar()!=EOF) {
        printf("\"getchar()!=EOF\" result is %d\n", d);
        printf("EOF:%d\n", EOF);
    }
        printf("\"getchar()!=EOF\" result is %d\n", d);
}

EOF 不是一个字符。这EOF是一个宏getchar()当到达输入末尾或遇到某种错误时返回。这^D不是“EOF 字符”。在 Linux 下,当您在一行中单独点击 ^D 时,会发生什么情况,它会关闭流,并且getchar()调用到达输入末尾并返回EOF宏。如果您输入^D在一行中间的某个地方,流没有关闭,所以getchar()返回它读取的值并且循环不会退出。

See the stdioC 常见问题解答部分 http://www.c-faq.com/stdio/index.html为了更好的描述。

此外:

在现代系统上,它不反映存储在文件中的任何实际文件结束字符;这是没有更多字符可用的信号。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ctrl-d 没有停止 while(getchar()!=EOF) 循环[重复] 的相关文章

随机推荐