Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getch is deprecated

Tags:

c

deprecated

Somewhere back in time I did some C and C++ in college, but I didn't get to many attention to C++. Now I wish to pay some attention to C++ but when I'm using the getch() function, I get the warning from below.

Warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.

Now, I'm using VS 2005 express, and I don't know what to do about this warning. I need to use getch() after I printf() an error message or something else which require a key hit.

Can you help me with that?

like image 1000
Duncan Benoit Avatar asked May 02 '09 14:05

Duncan Benoit


People also ask

What can be substituted for Getch?

try this scanf(" %c",&a); also you can use getchar() but note that both of these functions( scanf and getchar() ) will leave a \n in buffer and so in your next enter to loop , they will take that as input ,which means you can use two getchar() , or add a space in scanf like above.

What happens if we don't use getch in C?

getch() is used to hold the screen.It makes sure that you can see your output on the output screen. If you don't use getch() at the end of your c program then you won't be able to see the output of your program.

Is Getch necessary in C?

No it is not at all important to write getch(). This function is used to get a single character input from the user” during execution of program. It also force to wait the output to stay on screen until any key pressed from keyboard or is used to hold the screen so that you are able to see the output.

Can we use getch in C ++?

Basic Syntax of getch() in C/C++ h> header file, so you must include it in your program. This function does not take any parameters. Here, getch() returns the ASCII value of the character read from stdin . For example, if we give the character '0' as input, it will return the ASCII value of '0', which is 49.


3 Answers

Microsoft decided to mark the name without underscore deprecated, because those names are reserved for the programmer to choose. Implementation specific extensions should use names starting with an underscore in the global namespace if they want to adhere to the C or C++ Standard - or they should mark themselves as a combined Standard compliant environment, such as POSIX/ANSI/ISO C, where such a function then corresponds to one of those Standards.

Read this answer about getcwd() too, for an explanation by P. J. Plauger, who knows stuff very well, of course.

If you are only interested to wait for some keys typed by the user, there really is no reason not to use getchar. But sometimes it's just more practical and convenient to the user to use _getch and friends. However, those are not specified by the C or C++ Standard and will thus limit the portability of your program. Keep that in mind.

like image 165
Johannes Schaub - litb Avatar answered Nov 16 '22 00:11

Johannes Schaub - litb


If you're into C++, why printf and getch? Consider using cout and cin.get instead.

like image 29
Eli Bendersky Avatar answered Nov 15 '22 22:11

Eli Bendersky


use _getch() instead of getch()

like image 5
shahzaib Avatar answered Nov 16 '22 00:11

shahzaib