how does getchar() know that enter has been pressed?

The following program copies the input to output.
1
2
3
4
5
6
7
8
9
10
main()
{
  int c;
  c = getchar();
  while(c != EOF){
    putchar();
    c = getchar();
   }
}


Why is the output printed after every hit of return key?
Isn't getchar holding only one value, so how does the program convey the information that enter has been pressed? Who takes care of this?
It doesn't, directly. A newline is just a character like any other.

However, the putchar writes to stdout. stdout is usually buffered.
http://www.cplusplus.com/reference/cstdio/stdout/

That optional&partial buffering, on your platform, probably flushes the buffer whenever you do write a newline character. It doesn't have to do so on every platform.
Topic archived. No new replies allowed.