3

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;
}
Keval Domadia
  • 4,768
  • 1
  • 37
  • 64
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • `-1` is two characters: a minus sign and a digit 1. – pmg Feb 16 '13 at 09:14
  • Ah, your code is fine. Miracle of miracles, this is actually a bug in _Eclipse_: http://stackoverflow.com/questions/4711098/passing-end-of-transmission-ctrl-d-character-in-eclipse-cdt-console – Mooing Duck Apr 30 '15 at 18:34

4 Answers4

4

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.

Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • I tried that. but it didn't work in eclipse IDE. but outside eclipse, in terminal window, CTRL + D is working fine! Its sorted out now. – KawaiKx Feb 16 '13 at 08:54
  • http://stackoverflow.com/questions/4711098/passing-end-of-transmission-ctrl-d-character-in-eclipse-cdt-console – Mooing Duck Apr 30 '15 at 18:34
3

Send the EOF character.

In Linux in a terminal use CTRL-D.

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
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.

KawaiKx
  • 9,558
  • 19
  • 72
  • 111
-1

in Mac is Cmd + D also. Im pretty sure in windows is Windows + D