Hi, this is such a newbie question that I'm almost embarrassed to ask it :$ ... but anyway:
I've started reading "The C Programming Language" (K&R) and I have a doubt about the getchar() function.
For example this code:
#include <stdio.h>
main(){
int c;
c = getchar();
putchar(c);
printf("\n");
}
given the entry "toomanychars" + CTRL+D (EOF) prints just "t". I think that's expected since it's the first character introduced.
But then this other piece of code:
#include <stdio.h>
main(){
int c;
while((c = getchar()) != EOF)
putchar(c);
}
given "toomanychars" + CTRL+D (EOF) prints "toomanychars".
My question is, why does this happens if I only have a single char variable? where are the rest of the characters stored?
Thanks.
EDIT:
Thanks to everyone for the answers, I start to get it now... only one catch:
The first program exits when given CTRL+D while the second prints the whole string and then waits for more user input. Why does it waits for another string and does not exit like the first?