HI, I'm just new to c (programming in general), and I am wondering why getche() echoes every character that I type but not 'Enter' key. Isn't 'Enter' key a character? And also, why does getchar() -which requires to press enter after typing a character- echo the 'Enter' along with the typed character (while it shouldn't because that 'Enter' is for finishing getchar() function)?

Here is the code for getchar() function-


The output I get is-


I want to know why it echoes 'Enter' key when pressed after a character? And why getche() doesn't (when pressed as a character)?

It's just how the designers of those two functions decided they should work.

Maybe it does. The Enter character code might be 10 (ASCII) is 10 or 0x0a hexadecimal. Or it could be EOF which might be -1.

Now see what happens when your putchar that to the screen. It may not be what you expect at all.

Maybe you wanted the cursor to move to the beginning of the line and drop down a line. THAT would depend on the terminal and OS. Remember this all began back in 1969 so many new programmers stumble around this because they don't know the history. Or what the enter key is returning.

I didn't notice but I was to write- "Hello\n" instead of "Hello".

@Sappie, so you indirectly told me you want a newline when the Enter is pressed. You'll have to code that to happen.

That is, \n is not the character generated by the enter key.

@rproffitt I actually missed \n in my question, but I was talking about echoing the Enter key after pressing 'a' (the character for getchar). Why does getchar echo Enter while it is for terminating getchar? And why does getche not echo it?

It appears you didn't read the above replies. This area is well discussed and I see answers above and there are more on the web. I chimed in because I'm running into folk that never used a terminal so they never had to deal with character I/O and what terminals did back then.

Sure, echo out the Enter key. My bet is that it will do nothing visible. If you want the Enter key to do something, then code it to do what you needed.

Again. Enter does not equal \n (newline.)

The keyboard and monitor data is 'cooked', not raw, unless you turn on raw orocessing like with stty in UNIX. The enter key is a carriage return 0x0D or ctrl-M, but is cooked into carriage return and linefeed 0x0D0A ctrl-M ctrl-J. On the teletype, one makes the typehead go to the left end (slower, so a head start), the other makes the paper roller move (still slow).  A monitor emulates a tty printer. The real tty needed padding characters to allow time for the motions, too! See 'man stty'!