Why Ctrl-Z does not trigger EOF?

喜你入骨 提交于 2019-12-17 09:57:52

问题


Why Ctrl+Z does not trigger the loop to finish on the following small program?

#include <stdio.h>

main()
{
    int c;
    while ((c = getchar()) != EOF)
    {
        //nothing 
    }

    return 0;
}

If I enter: test^ZEnter, it does not get out of the loop.

I found related questions around (here and here) but none to explain it for C (not C++) under Windows.

Note: I use Visual Studio 2015 PRE on a windows 8.1


回答1:


You need to hit Enter and then use ctrl+Z and then Enter again.

or, you may also use F6




回答2:


EOF like you use it is not a character. It's the status in which that stream is.

I mean, heck, you even link this question, so you might as well read the accepted answer:

The underlying form of an EOF is a zero-length read.

It's not an "EOF character".

http://www.c-faq.com/stdio/getcharc.html cites a different case than yours, where someone stored the return value of getchar in a char. The underlying problem still occurs occasionally: different runtimes implement different values for the EOF integer (which is why I said, it's not an EOF character), and things love to go wrong. Especially in Visual C++, which is not a "real" C compiler but a C++ compiler with a compatibility mode, it seems things can go wrong.



来源:https://stackoverflow.com/questions/31261483/why-ctrl-z-does-not-trigger-eof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!