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.
#include <stdio.h> int getchar(void); int getc(FILE *stream);
Try it
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.
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
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 plainchar, either a valid byte (0xFFon platforms wherecharis signed) is mistaken forEOF, or the loop never ends on platforms wherecharis unsigned. The classic loop iswhile ((ch = getchar()) != EOF). getchar()equalsgetc(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
EOFagain. On a terminal, the user can type end-of-file (Ctrl+D on Unix, Ctrl+Z on Windows) and read again later afterclearerr(). - The newline stays in the stream. After
scanf("%d", &n)the newline typed by the user is still there, and the nextgetchar()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 usescanf()with checked results. - Classification functions from
<ctype.h>take anintthat is eitherEOFor anunsigned charvalue, so pass the result ofgetchar()to them directly, before you convert it tochar. - Binary data. On POSIX systems text and binary streams behave the same; reading arbitrary bytes with
getc()works, butfread()is faster for blocks.
Related
fgets,putcharandputc,ungetc,feofandferror,scanf- Lessons: User input, Characters, Text files