Gus Tabares wrote:
Hello,
I'm working through the K&R book but I can't seem to get over this
one little simple program:
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF)
{
putchar(c);
}
}
....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?
Not much. But main is a function of type int. To stay out of trouble in
comp.lang.c you would do it this way..
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}
Ok, now that we have the program fixed up, what's going on? EOF is a
macro which evaluates to a negative integer, maybe -1. EOF will be
returned at the end of the (stdin) stream. If stdin is your keyboard,
you must tell the implementation about the end of the stream. This is OS
specific but usually Cntrl-Z for Microsoft or Cntrl-D for *nix is
interpreted as EOF from the keyboard.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---