What is the Difference Between getc and getchar?
🆚 Go to Comparative Table 🆚The primary difference between getc
and getchar
lies in the input sources they read from. Here are the key differences:
- getc: This function reads a single character from any input stream and returns the corresponding integer value of the ASCII value of the read character. On failure, it returns EOF. The syntax for
getc
isint getc(FILE *stream);
. - getchar: This function reads a single input character from the standard input (keyboard). It is equivalent to
getc(stdin)
. The syntax forgetchar
isint getchar(void);
.
In summary, getc
can read from any input stream, while getchar
is specifically designed to read from the standard input (keyboard).
Comparative Table: getc vs getchar
The primary difference between getc()
and getchar()
is that getc()
can read from any input stream, while getchar()
reads a single input character from the standard input, making it equivalent to getc(stdin)
. Here is a table summarizing their differences:
Feature | getc() | getchar() |
---|---|---|
Input Source | Can read from any input stream, such as a file or standard input | Reads a single input character from the standard input (stdin) |
Syntax | int getc(FILE *stream) |
int getchar(void) |
Return Value | Returns the corresponding integer value of the ASCII value of the read character or EOF on failure | Returns the corresponding integer value of the ASCII value of the read character or EOF on failure |
Both functions are used to read a character from an input stream and return the corresponding integer value or EOF on failure. They are provided by the C programming language and are used to read characters from various input sources.
Read more: