Standard Input, Output, and Error

The standard input, output, and error for a process can be referenced by the file descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. To use the stdio library routines on either of these files, their corresponding file streams stdin, stdout, and stderr can also be used. Here are the definitions of all three:

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

All three file streams can be accessed without opening them in the same way that the corresponding file descriptor values can be accessed without an explicit call to open().

There are some standard I/O library routines that operate on the standard input and output streams explicitly. For example, a call to printf() uses stdin by default whereas a call to fprintf() requires the caller to specify a file stream. Similarly, a call to getchar() operates on stdin while a call to getc() requires the file stream to be passed. The declaration of getchar() could simply be:

#define getchar()   getc(stdin)

Get UNIX Filesystems: Evolution, Design, and Implementation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.