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.
5 .\" 2005-06-16 mtk, mentioned freopen()
7 .\" 2007-12-08, mtk, Converted from mdoc to man macros
9 .TH STDIN 3 2008-07-14 "Linux" "Linux Programmer's Manual"
11 stdin, stdout, stderr \- standard I/O streams
16 .BI "extern FILE *" stdin ;
17 .BI "extern FILE *" stdout ;
18 .BI "extern FILE *" stderr ;
21 Under normal circumstances every Unix program has three streams opened
22 for it when it starts up, one for input, one for output, and one for
23 printing diagnostic or error messages.
24 These are typically attached to
25 the user's terminal (see
27 but might instead refer to files or other devices, depending on what
28 the parent process chose to set up.
29 (See also the "Redirection" section of
32 The input stream is referred to as "standard input"; the output stream is
33 referred to as "standard output"; and the error stream is referred to
35 These terms are abbreviated to form the symbols
36 used to refer to these files, namely
42 Each of these symbols is a
44 macro of type pointer to
46 and can be used with functions like
53 are a buffering wrapper around Unix file descriptors, the
54 same underlying files may also be accessed using the raw Unix file
55 interface, that is, the functions like
60 On program startup, the integer file descriptors
61 associated with the streams
66 are 0, 1, and 2, respectively.
67 The preprocessor symbols
72 are defined with these values in
76 to one of these streams can change the file descriptor number
77 associated with the stream.)
79 Note that mixing use of
81 and raw file descriptors can produce
82 unexpected results and should generally be avoided.
83 (For the masochistic among you: POSIX.1, section 8.2.3, describes
84 in detail how this interaction is supposed to work.)
85 A general rule is that file descriptors are handled in the kernel,
86 while stdio is just a library.
87 This means for example, that after an
89 the child inherits all open file descriptors, but all old streams
90 have become inaccessible.
97 are specified to be macros, assigning to them is nonportable.
98 The standard streams can be made to refer to different files
99 with help of the library function
101 specially introduced to make it possible to reassign
106 The standard streams are closed by a call to
108 and by normal program termination.
115 macros conform to C89
116 and this standard also stipulates that these three
117 streams shall be open at program startup.
124 is line-buffered when it points to a terminal.
125 Partial lines will not
130 is called, or a newline is printed.
131 This can produce unexpected
132 results, especially with debugging output.
133 The buffering mode of the standard streams (or any other stream)
134 can be changed using the
141 is associated with a terminal, there may also be input buffering
142 in the terminal driver, entirely unrelated to stdio buffering.
143 (Indeed, normally terminal input is line buffered in the kernel.)
144 This kernel input handling can be modified using calls like