5

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?

IcyFlame
  • 5,059
  • 21
  • 50
  • 74

2 Answers2

9

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • `+1` for nice background explanation – Bartek Banachewicz Feb 23 '13 at 12:03
  • What kind of POSIX code would be broken? The POSIX `getch()` is a part of `curses`, so any POSIX code that relies on `getch()` would need a port of `curses` anyway. – n. m. could be an AI Feb 23 '13 at 12:08
  • 17.4.3.1.2 Global names 1 Certain sets of names and function signatures are always reserved to the implementation: — Each name that contains a double underscore (__) or begins with an underscore followed by an upper-case letter (2.11) is reserved to the implementation for any use. And this does not apply to _gecth. – Paul Feb 23 '13 at 12:11
  • @n.m. fair enough. I wasn't sure if `getch` was part of the POSIX spec, or if it came from some other library. I know they gave all POSIX functions this treatment, and a quick google of `getch` mentioned POSIX, so I figured it was a POSIX function. If it is not, that doesn't really change anything, since their rename wasn't confined to *just* POSIX names – jalf Feb 23 '13 at 12:15
  • 1
    @Paul: which version of the C++ standard is that? Under 17.6.4.3.2 in N3290, I have "Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace." (similar wording was in the C++98 spec) – jalf Feb 23 '13 at 12:17
  • @jalf: All versions of the C++ standard have similar rules. There are two different reserved sets of names. One is reserved for use as any kind of name (internal, external, macro, whatever), the other is reserved for use as a name in the global namespace. – n. m. could be an AI Feb 23 '13 at 13:17
2

Both of these functions are non-standard (i.e. 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.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135