1

What is the difference between nodelay() and cbreak() in ncurses ? And why use these functions because we have getch()! If i have understand getch() permits to get the key directly just after the user hit the key. So nodelay and cbreak is useless isn't it ?

klutt
  • 30,332
  • 17
  • 55
  • 95
Progear
  • 157
  • 8

1 Answers1

1

They are two completely different functions.

From the documentation:

Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program. The nocbreak routine returns the terminal to normal (cooked) mode.

...

The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.

So cbreak is if you want to disable line buffering, which you may want for many reasons. One is to avoid having to use fflush(stdout) after each printout. With line buffering enabled, a printout will normally not be visible on screen until a newline character is printed.

The description of nodelay is pretty self explanatory. getch will not wait, but return instantly irregardless if any key is pressed or not.

Community
  • 1
  • 1
klutt
  • 30,332
  • 17
  • 55
  • 95
  • So cbreak() disable line buffering but we are in blocking mode and nodelay() causes getch in particular to be a non-blocking call ? – Progear May 21 '19 at 19:28
  • Ok ok but i don't understand why when i execute that code => int main(void) { initscr(); printw("Blah"); refresh(); while (1); } It will display Blah although i don't use cbreak ? – Progear May 21 '19 at 20:22
  • No, but printw flushes after every printout (or something else that has the same effect) which is not the case for printf. – klutt May 21 '19 at 21:42
  • Or maybe it doesn't. I don't really know. One important thing here is that this is a typical thing where it works they way it should *most of the time*. I tried the exact code here: https://stackoverflow.com/q/16877264/6699433 and I did not manage to reproduce the problem. – klutt May 21 '19 at 22:07