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;
}
Karma
  • 4,688
  • 1
  • 34
  • 62
KawaiKx
  • 8,919
  • 18
  • 67
  • 102
  • `-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
  • 172,020
  • 25
  • 347
  • 381
3

Send the EOF character.

In Linux in a terminal use CTRL-D.

Patrick B.
  • 11,133
  • 8
  • 54
  • 96
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
  • 8,919
  • 18
  • 67
  • 102
-1

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