Because your terminal is line-buffered. getchar()
and putchar()
still only work on single characters but the terminal waits with submitting the characters to the program until you've entered a whole line. Then getchar()
gets the character from that buffer one-by-one and putchar()
displays them one-by-one.
Addition: that the terminal is line-buffered means that it submits input to the program when a newline character is encountered. It is usually more efficient to submit blocks of data instead of one character at a time. It also offers the user a chance to edit the line before pressing enter.
Note: Line buffering can be turned off by disabling canonical mode for the terminal and calling setbuf with NULL
on stdin
.