c programming check if key pressed without stopping program

You can use kbhit() to check if a key is pressed:

#include <stdio.h>
#include <conio.h> /* getch() and kbhit() */

int
main()
{
    char c;

    for(;;){
        printf("hello\n");
        if(kbhit()){
            c = getch();
            printf("%c\n", c);
        }
    }
    return 0;
}

More info here: http://www.programmingsimplified.com/c/conio.h/kbhit

Leave a Comment