Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C EOF and BUFSIZ constants

Reference for the stdio.h constants EOF, BUFSIZ, FILENAME_MAX, FOPEN_MAX, L_tmpnam and SEEK_SET: what they mean, their values and how to use them.

<stdio.h> defines a small set of macros that describe the limits and special values of the stream library. The most important is EOF, an integer constant expression with a negative value (it is -1 in practice) that functions return to say "end of file or error". BUFSIZ is the default buffer size, and the SEEK_* constants name the starting points for fseek().

Their exact numeric values are implementation defined, except for the guarantees below, so always use the names.

C
#include <stdio.h>

int c;
while ((c = getchar()) != EOF) { /* ... */ }

Try it

C What EOF really is

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 Why char is the wrong type for the result

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
#define EOF       /* negative integer constant expression */
#define BUFSIZ    /* default buffer size for setbuf */
#define FOPEN_MAX /* minimum number of files you can open at once, at least 8 */
#define FILENAME_MAX
#define L_tmpnam
#define TMP_MAX
#define SEEK_SET  /* start of file */
#define SEEK_CUR  /* current position */
#define SEEK_END  /* end of file */
Name Description
EOF Negative int returned by input functions at end-of-file or on error. Also returned by output functions on failure.
BUFSIZ Size of the buffer used by setbuf(); a good default for your own I/O buffers (8192 on glibc).
FOPEN_MAX Number of streams the implementation guarantees you can have open at the same time (at least 8, including the three standard ones).
FILENAME_MAX Size of a char array big enough for any file name the implementation supports; may be misleading for real paths.
L_tmpnam, TMP_MAX Size of the buffer for tmpnam() and how many unique names it can make. tmpnam() is unsafe: prefer tmpfile().
SEEK_SET, SEEK_CUR, SEEK_END The whence argument of fseek().

Errors / undefined behavior: none for the constants themselves. The classic bug is storing fgetc() results in a char.

Usage notes

  • Hold results in int. Input functions return an unsigned char value (0 to 255) or EOF. A char variable loses the difference: when char is signed, a real byte 0xFF becomes -1 and looks like EOF; when it is unsigned, EOF can never be matched and the loop runs forever.
  • EOF is not a character and does not exist in the file. It is just the return value; a terminal user can produce it with Ctrl+D (Linux, macOS) or Ctrl+Z (Windows).
  • EOF means two things. After it, call feof() and ferror() to see whether the stream really ended or failed.
  • wint_t has WEOF for wide characters; do not mix them.
  • BUFSIZ is a good I/O chunk size for fread()/fwrite() loops and as the default for setvbuf(), but it is only a hint about the stream's own buffer.
  • FILENAME_MAX is not PATH_MAX. On Linux FILENAME_MAX is 4096; POSIX PATH_MAX is the right limit for paths, and some file systems have none at all.
  • Not for sizes of your data. Use sizeof and size_t for those.