getchar reads the input stream, available only after pressing the ENTER key. until then you only see the echo result from the console. To achieve the result you want, you can use something like this
#include <stdio.h> #include <termios.h> #include <unistd.h> int getCHAR( ) { struct termios oldt, newt; int ch; tcgetattr( STDIN_FILENO, &oldt ); newt = oldt; newt.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newt ); ch = getchar(); putchar(ch); tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); return ch; } void main() { int c; c = getCHAR(); while (c != 'b') { putchar(c); c = getCHAR(); } }
source share