Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C fgetc() and fputc() functions

Reference for C fgetc() and fputc(): read and write one character at a time on any stream, check for EOF correctly and copy files byte by byte.

fgetc() reads the next character from a stream and fputc() writes one. They are the basic building blocks of character-level input and output: getchar() is fgetc(stdin) and putchar() is fputc(c, stdout), just shorter to type.

Both treat the character as an unsigned char converted to int. That is why fgetc() returns int: it needs one extra value, EOF, to say "nothing more to read", and EOF is negative so it cannot collide with a real character.

C
#include <stdio.h>

int fgetc(FILE *stream);
int fputc(int c, FILE *stream);

Try it

C Counting characters, lines and vowels

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.

C Copying a file with fgetc and fputc

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

C
int fgetc(FILE *stream);
int fputc(int c, FILE *stream);
Name Description
stream An open stream: readable for fgetc(), writable for fputc().
c The character to write; converted to unsigned char before it is written.

Returns: fgetc() returns the character read as an unsigned char converted to int, or EOF at end-of-file or on a read error. fputc() returns the character written, or EOF on a write error.

Errors / undefined behavior: EOF is returned for two different reasons; distinguish them with feof() and ferror(). A NULL or closed stream, or a stream opened in the wrong direction, is undefined behavior or an error.

Usage notes

  • Store the result in an int. With a plain char, either a real byte 0xFF is mistaken for EOF (where char is signed) or the loop never ends (where char is unsigned).
  • Do not test feof() first. while (!feof(f)) { c = fgetc(f); ... } processes a bogus extra character. Loop on the return value as shown above.
  • Speed. Each call is cheap because the stream is buffered, but fread()/fwrite() on blocks is faster for big files. The getc()/putc() variants may be macros that avoid the function call cost, but evaluate the stream argument more than once, so never pass *fp++.
  • Text vs binary. In text mode on Windows '\n' is translated to and from "\r\n". On Linux and macOS there is no difference.
  • fputc(c, stdout) equals putchar(c); the stream versions let you choose stderr or a file.
  • Whether output appears right away depends on buffering: see fflush.
  • Returning a value you wrote: the result of fputc() is usually ignored for terminal output, but check it for files, where a full disk makes it fail.
  • Complexity: constant time per character.