getc, fgetc, getchar, getw
Purpose
Gets a character or word from an input stream.
Library
Standard I/O Library (libc.a)
Syntax
#include <stdio.h>
int getc (stream) int getchar ( )
FILE *stream;
int getw (stream)
int fgetc (stream) FILE *stream;
FILE *stream;
Description
The getc macro returns the next character (byte) from the
input specified by the stream parameter and moves the
file pointer, if defined, ahead one character in stream.
getc is a macro and cannot be used where a subroutine is
necessary; for example, a subroutine pointer cannot point
to it.
Because it is implemented as a macro, getc does not work
correctly with a stream parameter that has side effects.
In particular, the following does not work:
getc(*f++)
In cases like this, use the fgetc subroutine instead.
The fgetc subroutine performs the same function as getc,
but fgetc is a genuine subroutine, not a macro. The
fgetc subroutine runs more slowly than getc, but takes
less space.
The getchar macro returns the next character from the
standard input stream, stdin. Note that getchar is also
a macro.
The getw subroutine returns the next word (int) from the
input specified by the stream parameter and increments
the associated file pointer, if defined, to point to the
next word. The size of a word varies from one machine
architecture to another. The getw subroutine returns the
constant EOF at end-of-file or when an error occurs.
Since EOF is a valid integer value, feof and ferror
should be used to check the success of getw. The getw
subroutine assumes no special alignment in the file.
Because of possible differences in word length and byte
ordering from one machine architecture to another, files
written using putw are machine-dependent and may not be
readable using getw on a different type of processor.
Return Value
These subroutines and macros return the integer constant
EOF at end-of-file or upon an error.
Related Information
In this book: "feof, ferror, clearerr, fileno," "fopen,
freopen, fdopen," "fread, fwrite," "gets, fgets,"
"NLgetctab," "putc, putchar, fputc, putw," "scanf,
fscanf, sscanf, NLscanf, NLfscanf, NLsscanf," and
"standard i/o library."