3
int main()
{
int c;

  while ( (c = getchar())  != EOF)
    putchar(c);

}

Now ,running the above program produces

$./a.out thisisthelinewhosestoragelocationisamysterytome -- LINE1 thisisthelinewhosestoragelocationisamysterytome -- LINE2

When i entered the characters of LINE1 , i think the functions getchar() and putchar() , have been processing the characters , or am i wrong ?

Here is my question.

After i hit enter , my LINE1 is duplicated exactly to LINE2 , which means it should have been buffered elsewhere , so where is it stored ? Also why is it implemented this way ?

Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • 1
    Check this out: http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar Compelling reason: This allows the user to modify the input (such as backspacing and retyping) before sending it to the program. – Bart Friederichs Mar 08 '13 at 12:39
  • 2
    There are _many_ buffers in the way from the keyboard to the output, including but not limited to the kernel, the terminal program, and the actual standard I/O functions – Some programmer dude Mar 08 '13 at 12:42
  • @JoachimPileborg , As i have not used windows for a long while , another question is if this type of multiple buffering linux specific ? – Barath Ravikumar Mar 08 '13 at 12:52
  • 1
    All operating systems and runtime systems have buffering. In fact it specified in the C standard that the I/O functions should be buffered by default. – Some programmer dude Mar 08 '13 at 12:56
  • @BartFriederichs , thanks for the link , i now got to know that a canonical mode is implemented in unix like systems , where the input is stored in the terminal buffer , and is dispatched to the program only if '\n' is encountered – Barath Ravikumar Mar 08 '13 at 13:16

3 Answers3

2

Your program doesn't receive input from the shell until you've entered a whole line.

James
  • 24,676
  • 13
  • 84
  • 130
2

The default behavior of the system is to buffer the input until it sees a newline so that you have the option of hitting backspace and making edits to the line before your program sees it.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • yes , so is the input buffer implemented such that , the termminal actually sends the data to program after seeing '\n' ? – Barath Ravikumar Mar 08 '13 at 12:51
  • @BarathBushan - No, the terminal program just launches your program. Your program then calls the OS to read from stdin. Your program can also ask that the input not be buffered, then it will get each keystroke as it arrives (including backspaces and such). I don't know the details of where the buffering happens (kernel, drivers, etc.). – Ferruccio Mar 08 '13 at 15:34
0

I will omit I/O mechanisms of the system to supply the input stream to your program and getting the output stream from it. (Since I don't know them)

The getchar() function simply retrieves one character from stdin. On the other way, putchar() just puts one character to the output stream (stdout). Thus, there is no buffer magic involved here, you're just getting what you would expect to get : a perfect copy of what has been put in stdin in stdout.

Rerito
  • 5,886
  • 21
  • 47