3

Possible Duplicate:
getch is deprecated

As title says, what are diffrences between those two methods? I'm new so I'm confused about the usage of them...

Community
  • 1
  • 1
ducnh
  • 260
  • 1
  • 3
  • 10
  • Which implementations of `getch` and `_getch` are you talking about? If you're talking about Microsoft's implementations, you really should look at [this URL](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch). – ArjunShankar Aug 17 '12 at 15:52
  • See http://stackoverflow.com/a/1587158/12711 – Michael Burr Aug 17 '12 at 15:53
  • 1
    When you read the documentation, **which part** did you find confusing? By articulating your question better, you'll get better answers, and people responsible for the documentation might be inspired to clarify it so people won't be confused anymore. – Rob Kennedy Aug 17 '12 at 16:01
  • @RobKennedy Kenedy and all: the part i confused is where i should use getch() and where i should use _getch() ? – ducnh Aug 17 '12 at 16:03

2 Answers2

4

At least in the implementations of which I'm aware, there's no difference between the functions themselves. In fact, they're generally just two different names for exactly the same function.

As to the reason for having two names: there really isn't a very good one. Somebody at Microsoft apparently didn't read the requirements of the standard very carefully, and made some rather...poor decisions based on misunderstanding it.

First, getch isn't declared in a standard header, so changing the name isn't really necessary to start with1. Second, if they did need to change the name, _getch isn't right anyway -- the names reserved for the implementation start with an underscore (they got that much right) followed by either another underscore or a capital letter (which they got wrong). In other words, if they were going to change the name, it should have been either __getch, or _Getch, but at least as far as the standard cares, _getch is just as bad as plain getch.

As far as choosing between them goes: I'd just use getch and be done with it. Using _getch actually makes your code (marginally) less portable -- to do the same thing on most Unixesque systems, you use curses, which includes a function to do (mostly) the same job -- and its name is getch. As such, if you ever port your code, you'll need to change the header(s) you include, but the name getch is one of the few that will actually continue to work. If you're doing much interactive I/O, you'll probably have to rewrite quite a bit of other code though.


1Well, it shouldn't be anyway. Back in the 16-bit days, Microsoft's linker had a little problem that you had to pass it an extra switch (/noe) or any duplicate between names you defined, and names defined in a library you were linking would result in an error. So, back then you had to pass an extra switch to get code to link if it used the same name as anything in the library, not just a standard name. That's pretty much ancient history though.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Identifiers don't necessarily have to start with two underscores or an underscore followed by an uppercase letter - those kinds of names are reserved by the implementation for *any* use. Names that start with an underscore are reserved for names in the global namespace (and sometimes for other uses depending on the specific C/C++ standard in effect). – Michael Burr Aug 18 '12 at 05:18
4

Very old implementations of Microsoft's C compilers provided functions that used names that were the same as those in the POSIX/UNIX world and/or infringed on the user's namespace. Note that some of these were 'infringements' were probably done from before standardization took place (in the MS-DOS days).

At some point a long time ago Microsoft decided to move these names to a a set of names that was reserved for the compiler implementation. They did this for many names in the library, even if they might not have had to, strictly speaking. It appears that they did this pretty much for all library names that weren't standard C/C++. Note that this doesn't apply to names in the SDK - that set of headers and libraries is outside the compiler implementation domain, even if the SDK is distributed with the compiler.

For compatibility with programs written with the old names (the ones without the underscore) Microsoft provides a library, oldnames.lib, that implements aliases which link the old names (such as getch) to the new names (_getch). Both names refer to the exact same code. So as far as making things work, you can use either name (though you may need to set up your project to link in oldnames.lib).

If you have old Windows code that uses the old names, I think it's probably best to just link in oldnames.lib and be done.

For new code, I think using the new names (with the underscore and without linking in oldnames.lib) might be marginally better. The old names are deprecated by Microsoft, and all else being equal that should probably tip the scales. And if you end up porting your code to a POSIX system or porting POSIX code to Windows, there's a better chance you'll be alerted to areas that may need attention. The functions may look and mostly act like the POSIX versions, but they may need to be used in slightly different ways - particularly in error handling.

Or you could try a library like PDCurses or use your own wrappers to provide portability, depending on how much that might be worth to you. Or if you really want POSIX portability on Windows, Cygwin might be an option (is Services for Unix still around?).

Some examples of subtle differences between MSVC and POSIX functions which have the same/similar names:

  • getch() on Windows doesn't ever echo the character, always blocks until there is input, requires multiple calls to read some keys, and cannot return an error. Those behaviors differ than on POSIX.
  • ungetch() on Windows returns the character passed in or EOF on error. On POSIX it returns either OK or ERR.
Michael Burr
  • 333,147
  • 50
  • 533
  • 760