What is the difference between getch(), getche() and getchar() functions?

Hi Everybody,
I am a beginner in C++ and I was wondering that what is the difference between getch(), getche() and getchar() functions.

I read somewhere that getch() gets a character from stdin. What does this mean? What is stdin?

Waiting for your reply,
Thank you,
-Himansh
closed account (Dy7SLyTq)
stdin is the standard input stream.
not accurate, but you can think of the stdin as the keyboard.

i assume getch() and getche() are the ones in conio.h.
if so:
those two functions are for interactive input operations.
they both extract a character from the console and return the integer that corresponds to it.
the difference is:
getch() doesn't echo the extracted character to the console, means : the extracted character doesn't get printed on the screen.
getche() echos the extracted character to the console, means : the extracted character gets printed on the screen.
and "interactive" means that they don't wait for a (Enter) to be pressed to finish their job,
instead, they return as soon as any key is pressed.

getchar() extracts one character from stdin, but it waits until (Enter) is pressed, then returns.
it is described in the standard as being interactive, yet it waits for the (Enter) to return, this behaviour can't achieve real interactivity in some situations.

programmers consider getch() and getche() bad for being non-standard, but i think they complete a missing part of the C++ standard.
closed account (Dy7SLyTq)
i am completely accurate actually:
http://www.cplusplus.com/reference/cstdio/stdin/

the keyboard is the source of the input put in the buffer, but stdin is the stream for it
closed account (3qX21hU5)
You are in a C++ forum not C DTSCode ;p For C++ the standard input stream would be istream http://www.cplusplus.com/reference/istream/ . stdin is the standard input stream for C.

So not completely accurate.
Last edited on
closed account (Dy7SLyTq)
aha but look at my link: from this site and it says that but we were talking in terms of c so i was right for that
closed account (3qX21hU5)
I don't think we are talking in terms of C otherwise the OP wouldn't have said anything about C++ like he did he would have said C. I believe he just is doing what most beginners do and using C techniques in C++ which should generally be avoided.

So yes you were right that stdin is the stand input stream for C, but for C++ it is non standard and should be avoided which is why I am saying this so the OP doesn't get the idea that cstdin should be used over istream.

To the above generally I would stay away from getch(), getche() and getchar() if you are going to be programming in C++.
Last edited on
closed account (Dy7SLyTq)
hmm didnt see that. i saw the get*() stuff (that brings back memories) and stdin and thought we were talking about c
Thank you all your replies. When we use getch() or getche() and press Enter when it is prompting for input, we get an ascii code 13(\n). But, when we use getchar(), we get ascii code 10. Why this happens?
getchar() is the only standard C++ function out of the three. It returns an int so that is why it prints the ascii code. If you want to print it as a character you'll have to cast it to a char.
well...this is the downside of these functions:
they work for letters and numbers.
if you press a control key (enter, shift, arrow, delete, F1, ....etc.) you'll likely get weird and sometimes wrong result, so please don't try it.

as you can see:
- (13 = 0xd)ASCII is reserved for (Carriage return).
- (10 = 0xa)ASCII is reserved for (Line Feed).

i made a consumption, but i can't go around throwing consumption and hoping they are correct.

i'd be REALLY happy to read an explanation for this behaviour.
On the windows platform, a newline in a text document is usually represented by the pair of characters [CR][LF] that is, a carriage-return, linefeed pair.

On other platforms, a single character is used. However, when using standard C++ streams in text mode, automatic translation takes place on input and output, so that it looks as though the single '\n' character is used. To see the actual character codes open the file in binary mode and use unformatted I/0.

As for the keyboard, when using the non-standard functions in the <conio.h> library - first bear in mind that these are not standard, the functions are not guaranteed to be available on all compilers, and if they are available, behaviour is not guaranteed to be consistent from one compiler to another.

The escape key will usually give a code of 27 from function getch(), which can be useful for allowing a simple exit from a loop.

In the case of certain special keys such as the arrow keys, the code returned from getch() will be zero, in which case call the function a second time to identify the particular key - but bear in mind that a different compiler may give a different result.
Hmm, I see. Thank you all for your help.
Topic archived. No new replies allowed.