I've code:
int c;
while((c = getchar()) != EOF)
putchar(c);
As I know EOF is equal to -1 I read -1 from keyboard(input stream) and it must stop itself,but loop keeps repeating. why?
I've code:
int c;
while((c = getchar()) != EOF)
putchar(c);
As I know EOF is equal to -1 I read -1 from keyboard(input stream) and it must stop itself,but loop keeps repeating. why?
-1
is actually two separate characters -
and 1
which is not equivalent to EOF. Use Ctrl + d on Linux or Ctrl + z on Windows.
As @SouravKanta alludes to: your loop is reading individual characters one at a time, so if you enter -1, it reads the -
and then the 1
, neither of which indicates EOF. I believe Cntl-d is what you want, at least in *nix (you don't specify the platform).