My main intention was to make getchar
return as soon as it gets a character instead of waiting for the ENTER key. I tried this
int main()
{
setvbuf(stdin,NULL,_IONBF,0);
getchar();
return 0;
}
Comparing this with the prototype of setvbuf
setvbuf ( FILE * stream, char * buffer, int mode, size_t size );
it should set stdin
to unbuffered mode.
But still getchar()
keeps waiting for ENTER
I've seen related posts like this
Printing while reading characters in C
which are suggesting alternate methods to make stdin
unbuffered. But I am curious to know as to why setvbuf
method does not work
setvbuf()
before any "movement" on the stream ... so first thing inmain()
. – Dandysetvbuf()
. I tried your program with and withoutsetvbuf()
and the behaviour is different. Withoutsetvbuf()
all characters up to and including the ENTER are consumed (even though it is consumed only after typing ENTER); withsetvbuf()
only the first character is consumed, the remaining characters are used as a following bash command. – Dandy