getc(3S) getc(3S)
NAME
getc, getchar, fgetc, getw - get character or word from a stream
SYNOPSIS
#include <stdio.h>
int getc(FILE *stream);
int getchar(void);
int fgetc(FILE *stream);
int getw(FILE *stream);
DESCRIPTION
getc() returns the next character (i.e., byte) from the named input
stream as an unsigned char converted to an int. It also moves the file
pointer, if defined, ahead one character in stream. getchar() is
defined as getc(stdin). getc() and getchar() are macros.
fgetc() behaves like getc(), but is a function rather than a macro.
fgetc() runs more slowly than getc(), but it takes less space per
invocation and its name can be passed as an argument to a function.
getw() returns the next word (i.e., integer) from the named input
stream. getw() increments the associated file pointer, if defined, to
point to the next word. The size of a word is the size of an int type
number, and varies from machine to machine. getw() assumes no special
alignment in the file.
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
The functions fail if:
EAGAIN The ONONBLOCK flag is set for the file descriptor under-
lying stream, and the process is delayed.
EBADF The file descriptor underlying stream is not a valid file
descriptor opened for reading.
EINTR The read operation was terminated due to the receipt of a
signal; no data was transferred.
EIO A physical I/O error has occurred, or the process is in a
background process group attempting to read from its con-
trolling terminal, and either the process is ignoring or
blocking the SIGTTIN signal or the process group is
orphaned. This error may also be generated for implementa-
tion-dependent reasons.
Page 1 Reliant UNIX 5.44 Printed 11/98
getc(3S) getc(3S)
EOVERFLOW The file is a regular file and an attempt was made to read
at or beyond the offset maximum associated with the corre-
sponding stream.
The fgetc() function may fail if:
ENOMEM Insufficient storage space is available.
ENXIO A request was made of a non-existent device, or the
request was outside the capabilities of the device.
RESULT
These functions return the constant EOF at end-of-file or upon an
error and set the EOF or error indicator of stream, respectively.
Because EOF is a valid integer, ferror() or feof() should be used to
detect errors.
NOTES
If the integer value returned by getc(), getchar(), or fgetc() is
stored into a char type variable and then compared against the integer
constant EOF, the comparison may never succeed, because sign-extension
in transferring from char to int is implementation dependent.
The macro version of getc() evaluates a stream argument more than once
and may treat side effects incorrectly. In particular, getc(*f++) does
not work sensibly. Use fgetc() instead.
Because of possible differences in word length and byte ordering,
files written using putw() are implementation dependent, and may not
be read using getw() on a different processor.
Functions exist for all the above-defined macros. To get the function
form, the macro name must be undefined (e.g., #undef getc()).
SEE ALSO
fclose(3S), ferror(3S), fopen(3S), fread(3S), gets(3S), putc(3S),
scanf(3S), stdio(3S), ungetc(3S), lfs(5), stdio(5).
Page 2 Reliant UNIX 5.44 Printed 11/98