There are some Windows C functions that do not work on Mac — e.g. getch()
. If I run them in a Windows VM will they work? Or is there a way to get them to work on OS X?
Asked
Active
Viewed 1,081 times
1

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

BeardedWonder_30
- 21
- 7
-
If you run Windows code in a Windows VM (that happens to be hosted on a Mac), then your code will run OK. If you want to run the code natively on a Mac, you will have to work harder. There are bound to be other questions that cover the topic of 'How do I simulate Windows `getch()` on a Unix system?' — I'll try and find them. – Jonathan Leffler Sep 18 '16 at 01:15
-
I know with all the looking I did getchar was the popular answer. I could not find anything about getting getch to work. – BeardedWonder_30 Sep 18 '16 at 01:16
1 Answers
1
Use getchar(); It should work.
But if you really want to use the exact function then I recommend using windows vm or create your own method which replicates getch() and use it as a library on your mac/linux.

Zaira Zafar
- 108
- 1
- 8
-
There's a difference between plain `getchar()` and WIndows' `getch()`; the latter returns a character without waiting for a return to be entered, whereas `getchar()` doesn't usually return anything until a return is entered. – Jonathan Leffler Sep 18 '16 at 01:13
-
Getch() holds the screen till a character is inputted but doesn't display the character where as getchar() also prints the inputted character on the screen. – Zaira Zafar Sep 18 '16 at 01:30
-
1Yeah; the full set of nuances are complex, and there's `getche()` to echo the character too on Windows. The key^H^H^Hmost important point is that `getch()` returns when the next key stroke occurs, immediately, even if it is not the 'return' or 'enter' key that's pressed, whereas `getchar()` normally won't return anything until the 'return' key is pressed, possibly after a number of other characters have been typed (though the characters will usually have been echoed), and it will return the first of the entered characters. – Jonathan Leffler Sep 18 '16 at 01:37
-
There's nothing that replicates getch() completely for mac/linux. If you really want to use the exact function either do it by windows VM or create a method which replicates getch() and use it as a library on mac/linux. – Zaira Zafar Sep 18 '16 at 01:43
-
If you add some of that commentary (yours, or anything useful from mine) to your answer, it will make it more useful and probably worthy of an up-vote. As it stands, it's a bit too terse to be truly useful. – Jonathan Leffler Sep 18 '16 at 01:50
-