k&R,how getchar read EOF

隐身守侯 提交于 2019-12-23 11:36:08

问题


while reading from k&r i came across the following example

#include<stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
{
    putchar(c);
}
printf("hello");
}

doubt 1:when i am typing the character ctrl+z(EOF on my sys) . o/p is hello
but when i am typing the string of characters like abcdef^Zghijk
o/p is abcdef->(including the arrow) and waiting for user to enter i/p instead of terminating loop and print hello..


回答1:


ctrl+z is not EOF, it is just a way to tell your terminal to close the stream.

On Windows systems you need to write the ctrl+z as the first character of the line, otherwise the terminal considers it to be an ordinary character.



来源:https://stackoverflow.com/questions/5473190/kr-how-getchar-read-eof

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