Why do I need to press CTRL+D twice to break out of `while ((c=getchar())!=EOF)` in Ubuntu 14.10?

后端 未结 1 921
夕颜
夕颜 2021-01-15 16:59

I am new to C Programming and Ubuntu. I was reading the \"The C Programming Language\" by D.M Ritchie where I found the following code:

#include 

        
相关标签:
1条回答
  • 2021-01-15 17:25

    Quoting @Veritas's comment,

    On linux Ctrl-D only works when the buffer is already empty otherwise it just flushes it. Therefore unless he has pressed enter without any characters after that, he will have to press Ctrl-D twice.

    This explains the issue. You have to press it twice because you , after typing Hello, did not press the Enter to flush the input into the stdin. So the first time you press CTRL+D, it flushes the data into the stdin. The second time you press it, EOF is sent.

    0 讨论(0)
提交回复
热议问题