getchar(): Get character from stdin
So, it does not monitor your key presses, it monitors the standard input. The standard input is committed when you press ENTER. Before that you can edit it (BACKSPACE, DELETE, move in the line with HOME, END, and arrows).
That's why you have to press an ENTER.
If you're after a key pressed functionality, there's no platform independent one. For OnlineGDB (and linux) you have to check ncurses.
Have a look at this.