Next: fgetpos, Previous: fflush, Up: Stdio [Contents][Index]

### 4.10 `fgetc`, `fgetc_unlocked`—get a character from a file or stream

Synopsis

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

    #define _BSD_SOURCE
    #include <stdio.h>
    int fgetc_unlocked(FILE *fp);

    #include <stdio.h>
    int _fgetc_r(struct _reent *ptr, FILE *fp);

    #define _BSD_SOURCE
    #include <stdio.h>
    int _fgetc_unlocked_r(struct _reent *ptr, FILE *fp);

Description
Use `fgetc` to get the next single character from the file or stream identified by fp. As a side effect, `fgetc` advances the file’s current position indicator.

For a macro version of this function, see `getc`.

`fgetc_unlocked` is a non-thread-safe version of `fgetc`. `fgetc_unlocked` may only safely be used within a scope protected by flockfile() (or ftrylockfile()) and funlockfile(). This function may safely be used in a multi-threaded program if and only if they are called while the invoking thread owns the (FILE *) object, as is the case after a successful call to the flockfile() or ftrylockfile() functions. If threads are disabled, then `fgetc_unlocked` is equivalent to `fgetc`.

The functions `_fgetc_r` and `_fgetc_unlocked_r` are simply reentrant versions that are passed the additional reentrant structure pointer argument: ptr.

Returns
The next character (read as an `unsigned char`, and cast to `int`), unless there is no more data, or the host system reports a read error; in either of these situations, `fgetc` returns `EOF`.

You can distinguish the two situations that cause an `EOF` result by using the `ferror` and `feof` functions.

Portability
ANSI C requires `fgetc`.

`fgetc_unlocked` is a BSD extension also provided by GNU libc.

Supporting OS subroutines required: `close`, `fstat`, `isatty`, `lseek`, `read`, `sbrk`, `write`.

Next: fgetpos, Previous: fflush, Up: Stdio [Contents][Index]