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.

Similar Reads

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 header file....

feof() Function in C

The feof() function is used to check whether the file pointer to a stream is pointing to the end of the file or not. It returns a non-zero value if the end is reached, otherwise, it returns 0....