OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man3 / stdin.3
1 .\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
2 .\"
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()
6 .\"
7 .\" 2007-12-08, mtk, Converted from mdoc to man macros
8 .\"
9 .TH STDIN 3 2008-07-14 "Linux" "Linux Programmer's Manual"
10 .SH NAME
11 stdin, stdout, stderr \- standard I/O streams
12 .SH SYNOPSIS
13 .nf
14 .B #include <stdio.h>
15
16 .BI "extern FILE *" stdin ;
17 .BI "extern FILE *" stdout ;
18 .BI "extern FILE *" stderr ;
19 .fi
20 .SH DESCRIPTION
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
26 .BR tty (4)
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
30 .BR sh (1).)
31 .PP
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
34 as "standard error".
35 These terms are abbreviated to form the symbols
36 used to refer to these files, namely
37 .IR stdin ,
38 .IR stdout ,
39 and
40 .IR stderr .
41
42 Each of these symbols is a
43 .BR stdio (3)
44 macro of type pointer to
45 .IR FILE ,
46 and can be used with functions like
47 .BR fprintf (3)
48 or
49 .BR fread (3).
50 .PP
51 Since
52 .IR FILE s
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
56 .BR read (2)
57 and
58 .BR lseek (2).
59 .PP
60 On program startup, the integer file descriptors
61 associated with the streams
62 .IR stdin ,
63 .IR stdout ,
64 and
65 .I stderr
66 are 0, 1, and 2, respectively.
67 The preprocessor symbols
68 .BR STDIN_FILENO ,
69 .BR STDOUT_FILENO ,
70 and
71 .B STDERR_FILENO
72 are defined with these values in
73 \fI<unistd.h>\fP.
74 (Applying
75 .BR freopen (3)
76 to one of these streams can change the file descriptor number
77 associated with the stream.)
78 .PP
79 Note that mixing use of
80 .IR FILE s
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
88 .BR exec (3),
89 the child inherits all open file descriptors, but all old streams
90 have become inaccessible.
91 .PP
92 Since the symbols
93 .IR stdin ,
94 .IR stdout ,
95 and
96 .I stderr
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
100 .BR freopen (3),
101 specially introduced to make it possible to reassign
102 .IR stdin ,
103 .IR stdout ,
104 and
105 .IR stderr .
106 The standard streams are closed by a call to
107 .BR exit (3)
108 and by normal program termination.
109 .SH "CONFORMING TO"
110 The
111 .IR stdin ,
112 .IR stdout ,
113 and
114 .I stderr
115 macros conform to C89
116 and this standard also stipulates that these three
117 streams shall be open at program startup.
118 .SH NOTES
119 The stream
120 .I stderr
121 is unbuffered.
122 The stream
123 .I stdout
124 is line-buffered when it points to a terminal.
125 Partial lines will not
126 appear until
127 .BR fflush (3)
128 or
129 .BR exit (3)
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
135 .BR setbuf (3)
136 or
137 .BR setvbuf (3)
138 call.
139 Note that in case
140 .I stdin
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
145 .BR tcsetattr (3);
146 see also
147 .BR stty (1),
148 and
149 .BR termios (3).
150 .SH SEE ALSO
151 .BR csh (1),
152 .BR sh (1),
153 .BR open (2),
154 .BR fopen (3),
155 .BR stdio (3)