C exit from infinite loop on keypress

Ragnis picture Ragnis · Jul 18, 2011 · Viewed 26.5k times · Source

How can I exit from an infinite loop, when a key is pressed? Currently I'm using getch, but it will start blocking my loop as soon, as there is no more input to read.

Answer

Rudy Velthuis picture Rudy Velthuis · Jul 18, 2011

If you are using getch() from conio.h anyway, try to use kbhit() instead. Note that both getch() and kbhit() - conio.h, in fact - are not standard C.