Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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.

getch() is a way to get a user inputted character. It can be used to hold program execution, but the "holding" is simply a side-effect of its primary purpose, which is to wait until the user enters a character. They wouldn't have made getch() if holding program execution was its only purpose, since there are other ways to hold program execution. Also, keep in mind that getch() is not platform independent, so use at your own peril...

yes, getch() gets a character,it is correct , that means you have to give one input character,it is main work

but until you provide input character it waits, means until it return the value of the keypress

for getting character input ,you can use it anywhere in programme , but for to view the output of your program without having to open the output window( in Turbo C compiler) ,you can use it as last statement of your programme

There is a description of getch() here

It does return the value of the keypress as an int.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top