getc() function is the C library function, and it's used to read a character from a file that has been opened in read mode by fopen() function.

int getc( FILE * stream );

Return Value

  • getc() function returns the next requested object from the stream on success.
  • Character values are returned as an unsigned char cast to an int or EOF on the end of the file or error.
  • The function feof() and ferror() to distinguish between end-of-file and error must be used.
int main()
  FILE *fp = fopen("fileName.txt", "r");
  int ch = getc(fp);
  while (ch != EOF)
    //To display the contents of the file on the screen
    ch = getc(fp);
  if (feof(fp))
     printf("\n Reached the end of file.");
     printf("\n Something gone wrong.");
  return 0;