问题
This c code is from K&R. It is running continuously, even when I type -1. I am using eclipse in fedora 17. whats wrong with this code? how would it terminate? Even CTRL+D not working to end it!
#include<stdio.h>
int main(void)
{
int c;
c = getchar();
while(c != EOF)
{
putchar(c);
c = getchar();
}
return 0;
}
回答1:
It is running continuously, even when I type -1
On Unix you need to type Ctrl-D instead to signal end of file.
As Mooing Duck points out, this seems to be a bug in Eclipse.
回答2:
Send the EOF character.
In Linux in a terminal use CTRL-D.
回答3:
It turned out to be a problem with eclipse IDE. outside eclipse, code is running fine. CTRL+D for unix is the EOF (internally -1). There is a workaround in eclipse. for each application that needs EOF, goto RUN --> RUN CONFIGURATION... --> in the main tab scroll down and uncheck ' connect process input and output to a terminal'. now run the code. CTRL+D should work. for each application you have to apply this trick separately.
回答4:
in Mac is Cmd + D also. Im pretty sure in windows is Windows + D
来源:https://stackoverflow.com/questions/14908507/how-do-we-terminate-this-c-code