That's because of the fact that getchar()
is terminal io settings dependent. Since most terminals have line buffering enabled, it waits until you press enter. Using termios.h
, you can disable that. getch()
is windows-only.
Here is some code to do what getch()
does in Linux.
#include <termios.h>
char getch(void) {
/* get original settings */
struct termios new, old;
tcgetattr(0, &old);
new = old;
/* set new settings and flush out terminal */
new.c_lflag &= ~ICANON;
tcsetattr(0, TCSAFLUSH, &new);
/* get char and reset terminal */
char ch = getchar();
tcsetattr(0, TCSAFLUSH, &old);
return ch;
}
Also, why realloc(blah, 0)
? Why not just free(blah)
?.
Also, malloc(0)
is undefined behaviour. It can return NULL or give a unique pointer. Same with realloc(blah, 0)
.