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 .\" Polished a bit by aeb.
6 .\" %%%LICENSE_START(PUBLIC_DOMAIN)
7 .\" Placed in the Public Domain.
10 .\" 2005-06-16 mtk, mentioned freopen()
11 .\" 2007-12-08, mtk, Converted from mdoc to man macros
13 .TH STDIN 3 2017-09-15 "Linux" "Linux Programmer's Manual"
15 stdin, stdout, stderr \- standard I/O streams
20 .BI "extern FILE *" stdin ;
21 .BI "extern FILE *" stdout ;
22 .BI "extern FILE *" stderr ;
25 Under normal circumstances every UNIX program has three streams opened
26 for it when it starts up, one for input, one for output, and one for
27 printing diagnostic or error messages.
28 These are typically attached to
29 the user's terminal (see
31 but might instead refer to files or other devices, depending on what
32 the parent process chose to set up.
33 (See also the "Redirection" section of
36 The input stream is referred to as "standard input"; the output stream is
37 referred to as "standard output"; and the error stream is referred to
39 These terms are abbreviated to form the symbols
40 used to refer to these files, namely
46 Each of these symbols is a
48 macro of type pointer to
50 and can be used with functions like
57 are a buffering wrapper around UNIX file descriptors, the
58 same underlying files may also be accessed using the raw UNIX file
59 interface, that is, the functions like
64 On program startup, the integer file descriptors
65 associated with the streams
70 are 0, 1, and 2, respectively.
71 The preprocessor symbols
76 are defined with these values in
80 to one of these streams can change the file descriptor number
81 associated with the stream.)
83 Note that mixing use of
85 and raw file descriptors can produce
86 unexpected results and should generally be avoided.
87 (For the masochistic among you: POSIX.1, section 8.2.3, describes
88 in detail how this interaction is supposed to work.)
89 A general rule is that file descriptors are handled in the kernel,
90 while stdio is just a library.
91 This means for example, that after an
93 the child inherits all open file descriptors, but all old streams
94 have become inaccessible.
101 are specified to be macros, assigning to them is nonportable.
102 The standard streams can be made to refer to different files
103 with help of the library function
105 specially introduced to make it possible to reassign
110 The standard streams are closed by a call to
112 and by normal program termination.
119 macros conform to C89
120 and this standard also stipulates that these three
121 streams shall be open at program startup.
128 is line-buffered when it points to a terminal.
129 Partial lines will not
134 is called, or a newline is printed.
135 This can produce unexpected
136 results, especially with debugging output.
137 The buffering mode of the standard streams (or any other stream)
138 can be changed using the
145 is associated with a terminal, there may also be input buffering
146 in the terminal driver, entirely unrelated to stdio buffering.
147 (Indeed, normally terminal input is line buffered in the kernel.)
148 This kernel input handling can be modified using calls like