getc : getc « stdio.h « C Tutorial






ItemValue
Header filestdio.h
Declarationint getc(FILE *stream);
Functionreads a character from stream.
Returnreturns EOF on file end or error.


When working with binary files, you must use ferror() to check errors.

The functions getc() and fgetc() are identical except that in most implementations getc() is defined as a macro.

(C: The Complete Reference, Fourth Edition by Herbert Schildt McGraw-Hill/Osborne 2000 (805 pages) ISBN:0072121246)

#include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;
    char ch;

    if((fp=fopen("test", "r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while((ch=getc(fp))!=EOF) {
      printf("%c", ch);
    }

    fclose(fp);
    return 0;
  }








22.20.getc
22.20.1.getc