I was going through K&R's the C Programming Language, and came across this bit of code:
main()
{
int c;
while ((c=getchar()) != EOF)
putchar(c);
}
I understand the reason the above code doesn't print one character at a time, immediately after getchar()
gets the character input from the stdin
, is because stdin
only sends the input to the program's input buffer after the user hits Enter
.
However, the function getchar()
still reads one character at a time from the input buffer, and passes said character to putchar()
to print. Why doesn't putchar()
print the character immediately after getting it? Does it also wait for a newline
character before printing, and store everything in its own buffer?