Both getchar()
and getch()
are functions in C programming language that are used for reading input characters from the keyboard. However, there are some differences between these two functions, which are summarized in the table below:
Function | getchar() | getch() |
---|---|---|
Header | #include <stdio.h> | #include <conio.h> |
Return type | int | int |
Input | Reads the next character from stdin | Reads the next character |
(standard input) | from the keyboard | |
Usage | Used for standard I/O operations | Used for console I/O operations |
Buffering | Uses buffered input, waits for Enter key | Does not use buffered input, |
to submit input | does not wait for Enter key | |
Compatibility | Works on all platforms including Windows, | Works only on Windows platform |
Linux, and macOS |
In summary, getchar()
reads the next character from standard input (usually the keyboard) and buffers the input until Enter key is pressed, while getch()
reads the next character from the keyboard without buffering and does not wait for the Enter key. getchar()
is a standard I/O function and is portable across different platforms, while getch()
is a console I/O function and is specific to the Windows platform.