1STDIN(3)                   Linux Programmer's Manual                  STDIN(3)
2
3
4

NAME

6       stdin, stdout, stderr - standard I/O streams
7

SYNOPSIS

9       #include <stdio.h>
10
11       extern FILE *stdin;
12       extern FILE *stdout;
13       extern FILE *stderr;
14

DESCRIPTION

16       Under  normal circumstances every UNIX program has three streams opened
17       for it when it starts up, one for input, one for output,  and  one  for
18       printing diagnostic or error messages.  These are typically attached to
19       the user's terminal (see tty(4)) but might instead refer  to  files  or
20       other  devices,  depending  on what the parent process chose to set up.
21       (See also the "Redirection" section of sh(1).)
22
23       The input stream is referred to as "standard input"; the output  stream
24       is  referred  to as "standard output"; and the error stream is referred
25       to as "standard error".  These terms are abbreviated to form  the  sym‐
26       bols used to refer to these files, namely stdin, stdout, and stderr.
27
28       Each  of these symbols is a stdio(3) macro of type pointer to FILE, and
29       can be used with functions like fprintf(3) or fread(3).
30
31       Since FILEs are a buffering wrapper around UNIX file  descriptors,  the
32       same  underlying  files  may  also  be accessed using the raw UNIX file
33       interface, that is, the functions like read(2) and lseek(2).
34
35       On program startup, the integer file descriptors  associated  with  the
36       streams  stdin,  stdout, and stderr are 0, 1, and 2, respectively.  The
37       preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are
38       defined  with  these values in <unistd.h>.  (Applying freopen(3) to one
39       of these streams can change the file descriptor number associated  with
40       the stream.)
41
42       Note  that  mixing  use  of  FILEs and raw file descriptors can produce
43       unexpected results and should generally be avoided.  (For the masochis‐
44       tic  among  you:  POSIX.1,  section 8.2.3, describes in detail how this
45       interaction is supposed to work.)  A general rule is that file descrip‐
46       tors  are  handled  in the kernel, while stdio is just a library.  This
47       means for example, that after an exec(3), the child inherits  all  open
48       file descriptors, but all old streams have become inaccessible.
49
50       Since the symbols stdin, stdout, and stderr are specified to be macros,
51       assigning to them is nonportable.  The standard streams can be made  to
52       refer  to different files with help of the library function freopen(3),
53       specially introduced to make it possible to reassign stdin, stdout, and
54       stderr.   The  standard  streams are closed by a call to exit(3) and by
55       normal program termination.
56

CONFORMING TO

58       The stdin, stdout, and stderr macros conform to C89 and  this  standard
59       also  stipulates  that  these  three  streams  shall be open at program
60       startup.
61

NOTES

63       The stream stderr is unbuffered.  The stream  stdout  is  line-buffered
64       when  it  points  to  a  terminal.  Partial lines will not appear until
65       fflush(3) or exit(3) is called, or a newline is printed.  This can pro‐
66       duce unexpected results, especially with debugging output.  The buffer‐
67       ing mode of the standard streams (or any other stream) can  be  changed
68       using  the  setbuf(3)  or  setvbuf(3) call.  Note that in case stdin is
69       associated with a terminal, there may also be input  buffering  in  the
70       terminal  driver, entirely unrelated to stdio buffering.  (Indeed, nor‐
71       mally terminal input is line buffered  in  the  kernel.)   This  kernel
72       input  handling can be modified using calls like tcsetattr(3); see also
73       stty(1), and termios(3).
74

SEE ALSO

76       csh(1), sh(1), open(2), fopen(3), stdio(3)
77

COLOPHON

79       This page is part of release 5.07 of the Linux  man-pages  project.   A
80       description  of  the project, information about reporting bugs, and the
81       latest    version    of    this    page,    can     be     found     at
82       https://www.kernel.org/doc/man-pages/.
83
84
85
86Linux                             2017-09-15                          STDIN(3)
Impressum