1 .\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
3 .\" This man page was written in 1998 by David A. Holland
4 .\" and placed in the Public Domain. Polished a bit by aeb.
13 .Nd standard I/O streams
15 .Fd #include <stdio.h>
16 .Fd extern FILE *stdin;
17 .Fd extern FILE *stdout;
18 .Fd extern FILE *stderr;
20 Under normal circumstances every Unix program has three streams opened
21 for it when it starts up, one for input, one for output, and one for
22 printing diagnostic or error messages. These are typically attached to
23 the user's terminal (see
25 but might instead refer to files or other devices, depending on what
26 the parent process chose to set up. (See also the ``Redirection'' section of
29 The input stream is referred to as ``standard input''; the output stream is
30 referred to as ``standard output''; and the error stream is referred to
31 as ``standard error''. These terms are abbreviated to form the symbols
32 used to refer to these files, namely
38 Each of these symbols is a
40 macro of type pointer to FILE, and can be used with functions like
45 Since FILEs are a buffering wrapper around Unix file descriptors, the
46 same underlying files may also be accessed using the raw Unix file
47 interface, that is, the functions like
51 The integer file descriptors associated with the streams
56 are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO,
57 STDOUT_FILENO, and STDERR_FILENO are defined with these values in
60 Note that mixing use of FILEs and raw file descriptors can produce
61 unexpected results and should generally be avoided.
62 (For the masochistic among you: POSIX.1, section 8.2.3, describes
63 in detail how this interaction is supposed to work.)
64 A general rule is that file descriptors are handled in the kernel,
65 while stdio is just a library. This means for example, that after an
66 exec, the child inherits all open file descriptors, but all old streams
67 have become inaccessible.
74 are specified to be macros, assigning to them is non-portable.
75 The standard streams can be made to refer to different files
76 with help of the library function
78 specially introduced to make it possible to reassign
83 The standard streams are closed by a call to
85 and by normal program termination.
95 is unbuffered. The stream
97 is line-buffered when it points to a terminal. Partial lines will not
102 is called, or a newline is printed. This can produce unexpected
103 results, especially with debugging output.
104 The buffering mode of the standard streams (or any other stream)
105 can be changed using the
112 is associated with a terminal, there may also be input buffering
113 in the terminal driver, entirely unrelated to stdio buffering.
114 (Indeed, normally terminal input is line buffered in the kernel.)
115 This kernel input handling can be modified using calls like
129 and this standard also stipulates that these three
130 streams shall be open at program startup.