Previous Page
Next Page

getc

Reads a character from a file

#include <stdio.h>
int getc ( FILE *fp  );

The getc( ) function is the same as fgetc( ), except that it may be implemented as a macro, and may evaluate its argument more than once. If the argument is an expression with side effects, use fgetc( ) instead.

getc( ) returns the character read. A return value of EOF indicates an error or an attempt to read past the end of the file. In these cases, the function sets the file's error or end-of-file flag as appropriate.

Example

FILE *inputs[16];
int nextchar, i = 0;

/* ... open 16 input streams ... */

do {
  nextchar = getc( inputs[i++] );       // Warning: getc( ) is a macro!
  /* ... process the character ... */
} while (i < 16);

The do...while statement in this example skips over some files in the array if getc( ) evaluates its argument more than once. Here is a safer version, without side effects in the argument to getc( ):

for ( i = 0; i < 16; i++ ) {
  nextchar = getc( inputs[i] );
  /* ... process the character ... */
}

See Also

fgetc( ), fputc( ), putc( ), putchar( ); the C99 functions to read and write wide characters: getwc( ), fgetwc( ), and getwchar( ), putwc( ), fputwc( ), and putwchar( ), ungetc( ), ungetwc( )


Previous Page
Next Page