Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly getch() does in C?

Tags:

c

getch

turbo-c

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?

like image 431
cipher Avatar asked Dec 02 '22 20:12

cipher


2 Answers

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.

like image 163
dreamlax Avatar answered Dec 11 '22 06:12

dreamlax


Well as you know, getch() reads a single byte character from input.

In those great days of Turbo C compiler,

Common use of getch is that you can view the output (if any) of your program without having to open the output window if you are using turbo c compiler or if you are not running your program from command prompt

Ah, those were days!!

BTW getch is said to be deprecated.

like image 44
Krishnabhadra Avatar answered Dec 11 '22 06:12

Krishnabhadra