How do we terminate this C code?

无人久伴 提交于 2019-12-10 19:35:32

问题


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

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