Using a non-standard I/O function isn't bad practice per se; it becomes an issue if you intend to port this code to a different platform that doesn't support getch()
.
The problem is, there are no standard library functions that detect individual keystrokes (getchar()
and fgetc()
are buffered and require you to hit Enter before they return); you're going to have to use something non-standard to do that. If you only intend for this code to run on this specific platform, use getch()
if that's what you need and just remember that this solution won't necessarily apply to other platforms. If you intend to port this code to platforms that don't support getch()
, isolate the system-specific call behind a wrapper function so that when you port the code, only the code inside the wrapper function needs to change.
int getKeystroke(void)
{
/**
* Put platform-specific call here
*/
return getch();
}
int main(void)
{
... // do stuff here
getKeystroke();
return 0;
}