The C Programming Language, Ansi C
In many environments, a file may be substituted for the keyboard by using the < convention for input redirection: if a program prog uses getchar, then the command line
prog <infile
causes
prog
to read characters from
infile
instead. The
switching of the input is done in such a way that
prog
itself is
oblivious to the change; in particular, the string
infile
'' is
not included in the command-line arguments in
argv
. Input switching
is also invisible if the input comes from another program via a pipe
mechanism: on some systems, the command line
otherprog | prog
runs the two programs
otherprog
and
prog
, and pipes the
standard output of
otherprog
into the standard input for
prog
.
The function
int putchar(int)
is used for output:
putchar(c)
puts the character
c
on the
standard output
, which is by default the screen.
putchar
returns the character written, or
EOF
is an error occurs. Again,
output can usually be directed to a file with >
filename
: if
prog
uses
putchar
,
prog >outfile
will write the standard output to
outfile
instead. If pipes are
supported,
prog | anotherprog
puts the standard output of
prog
into the standard input of
anotherprog
.
Output produced by printf also finds its way to the standard output. Calls to putchar and printf may be interleaved - output happens in the order in which the calls are made.
Each source file that refers to an input/output library function must contain the line
before the first reference. When the name is bracketed by < and > a
search is made for the header in a standard set of places (for example, on
UNIX systems, typically in the directory
/usr/include
).