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.
#include <stdio.h> int c; while ((c = getchar()) != EOF) { /* ... */ }
Try it
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.
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
#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 anunsigned charvalue (0 to 255) orEOF. Acharvariable loses the difference: whencharis signed, a real byte0xFFbecomes-1and looks likeEOF; when it is unsigned,EOFcan never be matched and the loop runs forever. EOFis 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).EOFmeans two things. After it, callfeof()andferror()to see whether the stream really ended or failed.wint_thasWEOFfor wide characters; do not mix them.BUFSIZis a good I/O chunk size forfread()/fwrite()loops and as the default forsetvbuf(), but it is only a hint about the stream's own buffer.FILENAME_MAXis notPATH_MAX. On LinuxFILENAME_MAXis 4096; POSIXPATH_MAXis the right limit for paths, and some file systems have none at all.- Not for sizes of your data. Use
sizeofandsize_tfor those.