Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getch() and _getch()

Tags:

c++

What is the difference between the two function that are defined in the conio.h header file-

getch() and _getch().

Is there a difference in the decalaration? Or is it simply a difference due to updated standards?

like image 349
IcyFlame Avatar asked Feb 23 '13 11:02

IcyFlame


1 Answers

It is part of a decision by Microsoft a couple of years ago to interpret the C++ standard more rigidly. It says that all names in the global namespace which start with an underscore are reserved for use by the implementation. This means that getch is not a reserved name, but _getch is.

So Microsoft figured that "this function, and every other POSIX function, is kind of supplied by the implementation. Let's rename them by prepending an underscore, so we're able to keep it inside the "reserved" part of the global namespace. That way, there's no chance of name clashes with user-supplied functions.

You could say that these are good intentions, or that it's just an evil attempt at breaking POSIX code. I don't know what their true motivation was, but the end result is that according to Microsoft, getch is deprecated, and you should use _getch instead.

like image 123
jalf Avatar answered Oct 06 '22 16:10

jalf