简体   繁体   中英

Difference between getch() and _getch()

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?

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.

Both of these functions are non-standard (ie they are not in C++ ISO standard). They are extensions provided by your particular toolchain, and as such, you have to check for differences in its documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM