Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C getchar() and getc() functions

Reference for C getchar(), getc() and fgetc(): read one character at a time from stdin or a file, why the result must be an int, and how to detect EOF.

getchar() reads the next character from standard input. getc() reads the next character from any input stream, and fgetc() does the same as a guaranteed function. The character comes back as an unsigned char converted to int, which leaves room for one extra value: EOF, returned when there is nothing more to read or an error occurs.

That extra value is why the result belongs in an int. If you store it in a char, you can no longer tell the end of the input from a real character.

C
#include <stdio.h>

int getchar(void);
int getc(FILE *stream);

Try it

C Counting characters, words and lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Reading a number digit by digit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int getchar(void);
int getc(FILE *stream);
int fgetc(FILE *stream);
Name Description
stream An open stream that allows reading: stdin, or the result of fopen() with a read mode.

Returns: the next character as an unsigned char converted to int, so a value from 0 to UCHAR_MAX; or EOF (a negative constant, usually -1) at end of file or on a read error.

Errors / undefined behavior: a NULL, closed, or write-only stream is undefined behavior. When EOF is returned, use feof() and ferror() to find out why.

Usage notes

  • Always store the result in an int. With a plain char, either a valid byte (0xFF on platforms where char is signed) is mistaken for EOF, or the loop never ends on platforms where char is unsigned. The classic loop is while ((ch = getchar()) != EOF).
  • getchar() equals getc(stdin). getc() may be implemented as a macro that evaluates its argument more than once, so keep the stream expression free of side effects. fgetc() is a real function.
  • After EOF, reads keep failing. Once the end of a file is reached, further reads return EOF again. On a terminal, the user can type end-of-file (Ctrl+D on Unix, Ctrl+Z on Windows) and read again later after clearerr().
  • The newline stays in the stream. After scanf("%d", &n) the newline typed by the user is still there, and the next getchar() returns it. See scanf pitfalls for how to drain it.
  • Pushing back. ungetc() puts one character back; it is how a parser "peeks" at the next character.
  • Buffering. Standard input is usually line buffered on a terminal: getchar() returns nothing until the user presses Enter. This is the terminal, not C.
  • Line-oriented input is safer. For whole lines use fgets(); for formatted values use scanf() with checked results.
  • Classification functions from <ctype.h> take an int that is either EOF or an unsigned char value, so pass the result of getchar() to them directly, before you convert it to char.
  • Binary data. On POSIX systems text and binary streams behave the same; reading arbitrary bytes with getc() works, but fread() is faster for blocks.