1stdin(3) Library Functions Manual stdin(3)
2
3
4
6 stdin, stdout, stderr - standard I/O streams
7
9 Standard C library (libc, -lc)
10
12 #include <stdio.h>
13
14 extern FILE *stdin;
15 extern FILE *stdout;
16 extern FILE *stderr;
17
19 Under normal circumstances every UNIX program has three streams opened
20 for it when it starts up, one for input, one for output, and one for
21 printing diagnostic or error messages. These are typically attached to
22 the user's terminal (see tty(4)) but might instead refer to files or
23 other devices, depending on what the parent process chose to set up.
24 (See also the "Redirection" section of sh(1).)
25
26 The input stream is referred to as "standard input"; the output stream
27 is referred to as "standard output"; and the error stream is referred
28 to as "standard error". These terms are abbreviated to form the sym‐
29 bols used to refer to these files, namely stdin, stdout, and stderr.
30
31 Each of these symbols is a stdio(3) macro of type pointer to FILE, and
32 can be used with functions like fprintf(3) or fread(3).
33
34 Since FILEs are a buffering wrapper around UNIX file descriptors, the
35 same underlying files may also be accessed using the raw UNIX file in‐
36 terface, that is, the functions like read(2) and lseek(2).
37
38 On program startup, the integer file descriptors associated with the
39 streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The
40 preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are
41 defined with these values in <unistd.h>. (Applying freopen(3) to one
42 of these streams can change the file descriptor number associated with
43 the stream.)
44
45 Note that mixing use of FILEs and raw file descriptors can produce un‐
46 expected results and should generally be avoided. (For the masochistic
47 among you: POSIX.1, section 8.2.3, describes in detail how this inter‐
48 action is supposed to work.) A general rule is that file descriptors
49 are handled in the kernel, while stdio is just a library. This means
50 for example, that after an exec(3), the child inherits all open file
51 descriptors, but all old streams have become inaccessible.
52
53 Since the symbols stdin, stdout, and stderr are specified to be macros,
54 assigning to them is nonportable. The standard streams can be made to
55 refer to different files with help of the library function freopen(3),
56 specially introduced to make it possible to reassign stdin, stdout, and
57 stderr. The standard streams are closed by a call to exit(3) and by
58 normal program termination.
59
61 C11, POSIX.1-2008.
62
63 The standards also stipulate that these three streams shall be open at
64 program startup.
65
67 C89, POSIX.1-2001.
68
70 The stream stderr is unbuffered. The stream stdout is line-buffered
71 when it points to a terminal. Partial lines will not appear until
72 fflush(3) or exit(3) is called, or a newline is printed. This can pro‐
73 duce unexpected results, especially with debugging output. The buffer‐
74 ing mode of the standard streams (or any other stream) can be changed
75 using the setbuf(3) or setvbuf(3) call. Note that in case stdin is as‐
76 sociated with a terminal, there may also be input buffering in the ter‐
77 minal driver, entirely unrelated to stdio buffering. (Indeed, normally
78 terminal input is line buffered in the kernel.) This kernel input han‐
79 dling can be modified using calls like tcsetattr(3); see also stty(1),
80 and termios(3).
81
83 csh(1), sh(1), open(2), fopen(3), stdio(3)
84
85
86
87Linux man-pages 6.04 2023-03-30 stdin(3)