What exactly getch() does in C?

cipher picture cipher · Dec 3, 2012 · Viewed 18k times · Source

I thought (upto now) that the function of getch() reads a character from input buffer (or keyboard, to be simple). But i had to argue with my lab instructor. They say that the only work of getch() is to hold the program execution. I know that getch() can be used that way. But i just wanted to know was it the real purpose it was invented? Or, Is it rarely used in getting one-character inputs?

Answer

dreamlax picture dreamlax · Dec 3, 2012

getch is used to “read a single-byte character from the terminal associated with the current window” as specified by POSIX. That is its primary function. It can be configured to be non-blocking (e.g. the function returns straight away if there is no input) so your lab instructor would be wrong to say that its only purpose is to pause program execution.

If you're talking about the getch() implemented by Turbo-C then I'm not sure of the blocking semantics, but if its primary purpose was to pause program execution the function would surely be named something more apt, such as pause() or waitkb() or something similar.