0
char ch;
while((ch=getc(stdin))!=EOF)
{
    putc(ch,stdout);
}

As we know that EOF character can be inputted by ctrl-z.

I ran the program two times:-

1- When I input ctrl-z, the loop gets terminated, which is acceptable.

2- When I input ctrl-z along with some other text like demo and then press ctrl-z, then the loop does not get terminated.

So my question is that why the loop is getting terminated only by inputing ctrl-z alone?

user2864740
  • 60,010
  • 15
  • 145
  • 220
kevin gomes
  • 1,775
  • 5
  • 22
  • 30

1 Answers1

2

EOF is not a character that you can put into a stream. It's a meta-control character that can be returned by getc, but can not be written. ctrl-z does not technically send EOF, it sends SIGTSTP to the process and getc is programmed to respond to it by returning EOF.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75