getting a char in c without pressing “enter”

2019-07-20 22:00发布

问题:

Is it possible? Using regular scanf or getchar won't work because I have to press "enter" after entering the char.

回答1:

You're approaching the domain of ncurses.



回答2:

The input stream in C is buffered.

A buffer is a place where data is temporarily held until it is ready to be processed.

The buffer is flushed (emptied) when one of three things happens:

  • The buffer is full.
  • Data is to be printed to the standard output.
  • The buffer is flushed explicitly. (Which is what pressing the enter key does).

You're going to have to use facilities not included in the standard library in order to have that functionality.



回答3:

The streaming functionality is getting in the way. There may be an fcntl option to turn off the buffering. Or you may have better luck using the lower level read call.



回答4:

It is not possible by using pure standard C. You will have to use a system-specific library like conio.h or the Windows API etc.



回答5:

Maybe getch() in conio.h is what you need, but note that it's non-standard.



标签: c scanf