getc() Function in C
The C getc() function is used to read a single character from the given file stream. It is implemented as a macro in <stdio.h> header file.
Syntax of getc()
int getc(FILE* stream);
Parameters
- stream: It is a pointer to a file stream to read the data from.
Return Value
- It returns the character read from the file stream.
- If some error occurs or the End-Of-File is reached, it returns EOF.
EOF, getc() and feof() in C
In C/C++, getc() returns the End of File (EOF) when the end of the file is reached. getc() also returns EOF when it fails. So, only comparing the value returned by getc() with EOF is not sufficient to check for the actual end of the file. To solve this problem, C provides feof() which returns a non-zero value only if the end of the file has reached, otherwise, it returns 0.