Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between getch() and _getch() [duplicate]

Tags:

c++

c

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...

like image 243
ducnh Avatar asked Oct 25 '25 22:10

ducnh


1 Answers

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.

like image 147
Jerry Coffin Avatar answered Oct 27 '25 12:10

Jerry Coffin