How do we terminate this code?

This c code is from K&R. It runs continuously even when I type -1. I am using eclipse on fedora 17. What is wrong with this code? how will it end? Even CTRL + D doesn't work to finish it!

#include<stdio.h>
int main(void)
{
    int c;

    c = getchar();
    while(c != EOF)
    {
        putchar(c);
        c = getchar();
    }

    return 0;
}

      

+3


source to share


4 answers


It runs continuously even when I print -1

On Unix, you need to type Ctrl-D instead to pass the end of the file.




As Mooing Duck points out, this seems to be a bug in Eclipse .

+4


source


Send an EOF character.



On Linux, use CTRL-D in the terminal.

+3


source


Turned out to be a problem with the Eclipse IDE. external eclipse, the code works fine. CTRL + D for unix is ​​EOF (inside -1). There is a workaround in eclipse. for each application that requires EOF go to RUN -> RUN CONFIGURATION ... -> in the main tab, scroll down and uncheck "connect process input and bring it to terminal". now run the code. CTRL + D should work. you must apply this trick separately for each application.

+3


source


Mac also has Cmd + D. I'm pretty sure on Windows it is Windows + D

-1


source







All Articles