kevin gomes
kevin gomes

Reputation: 1805

EOF character is not detected when typing line of text first?

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?

Upvotes: 0

Views: 63

Answers (1)

Sergey L.
Sergey L.

Reputation: 22542

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.

Upvotes: 2

Related Questions