What is the equivalent to getch() & getche() in Linux?

#include <unistd.h> #include <termios.h> char getch(void) { char buf = 0; struct termios old = {0}; fflush(stdout); if(tcgetattr(0, &old) < 0) perror(“tcsetattr()”); old.c_lflag &= ~ICANON; old.c_lflag &= ~ECHO; old.c_cc[VMIN] = 1; old.c_cc[VTIME] = 0; if(tcsetattr(0, TCSANOW, &old) < 0) perror(“tcsetattr ICANON”); if(read(0, &buf, 1) < 0) perror(“read()”); old.c_lflag |= ICANON; old.c_lflag |= ECHO; if(tcsetattr(0, TCSADRAIN, … Read more

How to avoid pressing Enter with getchar() for reading a single character only?

This depends on your OS, if you are in a UNIX like environment the ICANON flag is enabled by default, so input is buffered until the next ‘\n’ or EOF. By disabling the canonical mode you will get the characters immediately. This is also possible on other platforms, but there is no straight forward cross-platform … Read more