Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

k&R,how getchar read EOF

Tags:

c

getchar

while reading from k&r i came across the following example

#include<stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
{
    putchar(c);
}
printf("hello");
}

doubt 1:when i am typing the character ctrl+z(EOF on my sys) . o/p is hello
but when i am typing the string of characters like abcdef^Zghijk
o/p is abcdef->(including the arrow) and waiting for user to enter i/p instead of terminating loop and print hello..

like image 444
Tarun Avatar asked Mar 29 '11 13:03

Tarun


1 Answers

ctrl+z is not EOF, it is just a way to tell your terminal to close the stream.

On Windows systems you need to write the ctrl+z as the first character of the line, otherwise the terminal considers it to be an ordinary character.

like image 63
hrnt Avatar answered Oct 12 '22 11:10

hrnt