6

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

Community
  • 1
  • 1
Alex
  • 5,510
  • 8
  • 35
  • 54
  • 1
    @ARBY , CTRL+C doesn't send `EOF`, right? – Spikatrix Jul 07 '15 at 06:43
  • 1
    @CoolGuy Oh sorry, I meant ctrl+D as in *nix. – Raman Jul 07 '15 at 06:45
  • http://stackoverflow.com/questions/1782080/what-is-eof-in-the-c-programming-language – Ediac Jul 07 '15 at 06:56
  • This link might help (http://stackoverflow.com/questions/11378899/why-doesnt-the-eof-character-work-if-put-at-the-end-of-a-line) – Raman Jul 07 '15 at 07:02
  • @ARBY I knew about that post (see initial question) but is not very clear – Alex Jul 07 '15 at 07:34
  • @ARBY Isn't that the same link provided in this question? – Spikatrix Jul 07 '15 at 07:40
  • 4
    @Alex Your problem is clearly explained in [the first link you provide](http://stackoverflow.com/questions/5655112/why-do-i-require-multiple-eof-ctrlz-characters). In short, since there are characters to be flushed into the stdin, CTRL+Z and enter flushes those characters instead of sending EOF. In other words, CTRL+Z (windows, DOS) and CTRL+D (Linux, Unix, OSX etc) send EOF **if there are no more characters to be flushed to the `stdin`.** – Spikatrix Jul 07 '15 at 07:42
  • @CoolGuy Thanks, you should post that as an answer to accept it. – Alex Jul 08 '15 at 08:47

2 Answers2

5

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

or, you may also use F6

Raman
  • 2,735
  • 1
  • 26
  • 46
-4

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.

Community
  • 1
  • 1
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • So why does Ctrl+Z not trigger "the status in which that stream is" to EOF? – user2864740 Jul 07 '15 at 06:46
  • it probably does, but you're not checking for that. – Marcus Müller Jul 07 '15 at 06:47
  • 1
    How does one check for it with getchar() then? – user2864740 Jul 07 '15 at 06:48
  • That's like asking "how do I mine for fish". – Marcus Müller Jul 07 '15 at 06:48
  • 1
    Apparently [people like to do it](http://stackoverflow.com/questions/10720821/im-trying-to-understand-getchar-eof). – user2864740 Jul 07 '15 at 06:49
  • That's really really old legacy behaviour. I recommend going the standard compliant rout of checking whether `feof(stdin)` returns true. – Marcus Müller Jul 07 '15 at 06:51
  • Funny, [this man page also likes to mine for fish](http://linux.die.net/man/3/getchar). Please provide a "non legacy" reference for getchar/getc. – user2864740 Jul 07 '15 at 06:53
  • @user2864740: You're right, but read that function prototype very closely! What's the return type? It's an int, and I think that's the case exactly because they wanted to be able to read chars or return errors. Now, what they did is take the EOF for all errors and end-of-file. Using EOF to check against end-of-file has been frowned upon for quite a while. I'll add a reference. – Marcus Müller Jul 07 '15 at 06:58
  • 4
    The problem in the cited case is using `char` as the variable; the posted code uses `int`, correctly, which can detect the EOF "out-of-band" state. – user2864740 Jul 07 '15 at 07:07