Unbuffered character input

J

Jules

I am trying to understand the behavior of a short program. Hopefully
someone can tell me what's going on.

The program I wrote is:
#include <stdio.h>

main()
{
char c;
while(1)
{
c=getchar();
printf("The character pressed was %c\n",c);
if(c=='q')
break;
}
}


I run it, and start pressing keys, and nothing happens. When I hit
return, I see a series of lines on the screen, one for each character
I pressed. If one of those keys is 'q,' the program does not exit
until I press return. I guess this means the input is being
buffered. Seems simple enough.
Next, I tried running the same program, piping the output to less.
Now, I don't see any of the output until the program exit, at which
point less displays the entire output. The funny thing here is that
the input is not buffered: As soon as I press q, the program
terminates and all of the output is displayed by less.
Can someone explain to me why the input is buffered in the first case
but not in the second? Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.
 
S

Spiros Bousbouras

I am trying to understand the behavior of a short program. Hopefully
someone can tell me what's going on.

The program I wrote is:
#include <stdio.h>

main()
{
char c;
while(1)
{
c=getchar();
printf("The character pressed was %c\n",c);
if(c=='q')
break;

}
}

I run it, and start pressing keys, and nothing happens. When I hit
return, I see a series of lines on the screen, one for each character
I pressed. If one of those keys is 'q,' the program does not exit
until I press return. I guess this means the input is being
buffered. Seems simple enough.
Next, I tried running the same program, piping the output to less.
Now, I don't see any of the output until the program exit, at which
point less displays the entire output. The funny thing here is that
the input is not buffered: As soon as I press q, the program
terminates and all of the output is displayed by less.
Can someone explain to me why the input is buffered in the first case
but not in the second?

I presume you're running your programme on a
Unix (like) operating system. The answer to your
question has nothing to do with standard C which
is what we discuss here.

out_of_topic {
I presume the input becomes unbuffered
because less arranges things these way.
You will notice that if you do
a.out | cat
the input is again buffered.
}
Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.

This is question 12.5 in the FAQ.
http://c-faq.com/

For Unix specific information you can ask
at comp.unix.programmer
 
S

Spiros Bousbouras

I presume you're running your programme on a
Unix (like) operating system. The answer to your
question has nothing to do with standard C which
is what we discuss here.

out_of_topic {
I presume the input becomes unbuffered
because less arranges things these way.

^^^^
Eeeek.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top