1STDIN(3) BSD Library Functions Manual STDIN(3)
2
4 stdin, stdout, stderr — standard I/O streams
5
7 #include <stdio.h>
8 extern FILE *stdin;
9 extern FILE *stdout;
10 extern FILE *stderr;
11
13 Under normal circumstances every Unix program has three streams opened
14 for it when it starts up, one for input, one for output, and one for
15 printing diagnostic or error messages. These are typically attached to
16 the user's terminal (see tty(4)) but might instead refer to files or
17 other devices, depending on what the parent process chose to set up. (See
18 also the ``Redirection'' section of sh(1) .)
19
20 The input stream is referred to as ``standard input''; the output stream
21 is referred to as ``standard output''; and the error stream is referred
22 to as ``standard error''. These terms are abbreviated to form the symbols
23 used to refer to these files, namely stdin, stdout, and stderr.
24
25 Each of these symbols is a stdio(3) macro of type pointer to FILE, and
26 can be used with functions like fprintf(3) or fread(3).
27
28 Since FILEs are a buffering wrapper around Unix file descriptors, the
29 same underlying files may also be accessed using the raw Unix file inter‐
30 face, that is, the functions like read(2) and lseek(2).
31
32 On program startup, the integer file descriptors associated with the
33 streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The
34 preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are
35 defined with these values in <unistd.h>. (Applying freopen(3) to one of
36 these streams can change the file descriptor number associated with the
37 stream.)
38
39 Note that mixing use of FILEs and raw file descriptors can produce unex‐
40 pected results and should generally be avoided. (For the masochistic
41 among you: POSIX.1, section 8.2.3, describes in detail how this interac‐
42 tion is supposed to work.) A general rule is that file descriptors are
43 handled in the kernel, while stdio is just a library. This means for
44 example, that after an exec(), the child inherits all open file descrip‐
45 tors, but all old streams have become inaccessible.
46
47 Since the symbols stdin, stdout, and stderr are specified to be macros,
48 assigning to them is non-portable. The standard streams can be made to
49 refer to different files with help of the library function freopen(3),
50 specially introduced to make it possible to reassign stdin, stdout, and
51 stderr. The standard streams are closed by a call to exit(3) and by nor‐
52 mal program termination.
53
55 sh(1), csh(1), open(2), fopen(3), stdio(3)
56
58 The stream stderr is unbuffered. The stream stdout is line-buffered when
59 it points to a terminal. Partial lines will not appear until fflush(3) or
60 exit(3) is called, or a newline is printed. This can produce unexpected
61 results, especially with debugging output. The buffering mode of the
62 standard streams (or any other stream) can be changed using the setbuf(3)
63 or setvbuf(3) call. Note that in case stdin is associated with a termi‐
64 nal, there may also be input buffering in the terminal driver, entirely
65 unrelated to stdio buffering. (Indeed, normally terminal input is line
66 buffered in the kernel.) This kernel input handling can be modified
67 using calls like tcsetattr(3); see also stty(1), and termios(3).
68
70 The stdin, stdout, and stderr macros conform to ANSI X3.159-1989
71 (“ANSI C89”), and this standard also stipulates that these three streams
72 shall be open at program startup.
73
74Linux 2.0 March 24, 1998 Linux 2.0