Reputation: 127
#include <stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
putchar(c);
return 0;
}
In my limited exposure to C,as long as C!=EOF the while condition will get executed which essentially will print c and then wait for the next char and try to examine of that does or does not violate the condition. Hence for each char we put enter the putchar will execute. However when I ran the executable what seemed to happen was the print statement would work only when I pressed for new line (Enter) and it had a buffer of some sort which would print the whole line in the input. I fail to see any buffer in the code that will hold the chars until a new line is pressed,so what is being done here ? Where is these chars getting stored until a new line is pressed ?
Upvotes: 0
Views: 66
Reputation: 12668
The getchar()
and putchar()
functions are part of stdio
package, that makes buffered input/output.
This means that, despite of the number of characters you actually request (one only in your case) the buffering mechanism makes the characters to wait in the buffer until a whole buffer (or one line, in case stdin
comes from a tty device) is filled.
What is happening here is that, on first getchar()
a full buffer fill is requested, and filled only partially with the input line you keyed in. After that, all the getchar()
calls take individual characters from the buffer, until it is empty again, and at that time, another full buffer is requested.
This is made to make single char processing an efficient way of processing. That also happens on output. The putchar()
function only fills the buffer, until it is completely full (or, in case your output channel is to a tty device, until you request to output a \n
character) and when it is, the full buffer is output to the file/device.
Also, the terminal driver in all flavours of unix, works in line mode, what means that until you press the <ENTER>
key, you don't get anything sent to the program. This will allow you to correct mistakes made on keying, by the use of the erase key (backspace) and/or the kill (Cntrl-U) keys. This is simulated in Windows console applications, so you'll probably need the output device into raw mode before being able to input in a character based way.
If you want to make your program to read one character at a time, you need to bypass the buffers of stdio (via setbuf(NULL)
function call, or using the read(2)
system call to read one character, which can be done with:
char c;
int res = read(0, &c, 1);
if (res < 0) { /* error */
...
} else if (res == 0) { /* EOF in input */
...
} else { /* 1, as you requested only one char */
... /* one character was read. */
}
) and to put the terminal driver in raw mode (via tcsetattr()
and tcgetattr()
calls, see termios(4)
manual page for details on how to do this) before you do actual reading. In this case, you must return the terminal state to line mode before terminating the program, or you will run into trouble.
Upvotes: 1
Reputation: 153458
The buffering is happening in the the reading of data and the writing of data.
stdin
and stdout
, independently can be character, line or fully buffered.
In your common case, both are line buffered.
Input is not given to getchar()
until Enter ('\n'
) is hit. (Can you back-space input?) and output is not displayed until a '\n'
is printed.
Upvotes: 1
Reputation: 987
When reading from the console like this, your program does not actually receive any input at all until you press enter, and then it reads and processes the entire line at once.
Your program has no idea that those characters even exist until you press enter, at which point they are sent to the stdin buffer. From here, your loop will read and print each character (including the new line) until either the buffer is empty (meaning it will wait for more input), or the end of file is reached.
Upvotes: 2