Use C getc function to read character from console

Syntax

C getc function has the following syntax.

int getc(FILE *stream);

C getc function is from header file stdio.h.

Description

C getc function reads a character from stream and returns EOF on file end or error.

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

Example

Read character from console by using C getc function


#include <stdio.h>
#include <stdlib.h>
//ww w  . ja v a  2  s . co m
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;
}




















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h