getc Function

Get a character from the stream.

Include

<stdio.h>

Prototype

int getc(FILE *stream);

Argument

stream pointer to the open stream

Return Value

Returns the character read or EOF if a read error occurs or end-of-file is reached.

Remarks

getc is the same as the function fgetc.

Example

#include <stdio.h> /* for getc, printf, */
                   /* fopen, fclose,    */
                   /* FILE, NULL, EOF   */

int main(void)
{
  FILE *buf;
  char y;

  if ((buf = fopen("afile.txt", "r")) == NULL)
    printf("Cannot open afile.txt\n");
  else
  {
    y = getc(buf);
    while (y != EOF)
    {
      printf("%c|", y);
      y = getc(buf);
    }
    fclose(buf);
  }
}

Example Input

Contents of afile.txt (used as input):

Short
Longer string

Example Output

S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
|