When a C program is run as a command,
the arguments on the command line are made available
is the command name itself,
is always greater than 0.
The following program illustrates the mechanism:
it simply echoes its arguments
main(argc, argv) /* echo arguments */
for (i = 1; i < argc; i++)
printf("%s%c", argv[i], (i<argc-1) ? ' ' : '\n');
whose individual elements are pointers to arrays of characters;
so they can be treated as strings.
The program starts by printing
and loops until it has printed them all.
The argument count and the arguments
If you want to keep them around so other
routines can get at them, you must
copy them to external variables.
The ``Standard Input'' and ``Standard Output''
The simplest input mechanism is to read the ``standard input,''
which is generally the user's terminal.
returns the next input character each time it is called.
A file may be substituted for the terminal by
itself need know nothing about where its input
This is also true if the input comes from another program via
provides the standard input for
from the standard output of
when it encounters the end of file
on whatever you are reading.
is normally defined to be
but it is unwise to take any advantage
As will become clear shortly,
this value is automatically defined for you when
and need not be of any concern.
on the ``standard output,''
which is also by default the terminal.
The output can be captured on a file
writes the standard output on
is created if it doesn't exist;
if it already exists, its previous contents are overwritten.
puts the standard output of
into the standard input of
which formats output in various ways,
may be intermixed in any order;
the output will appear in the order of the calls.
provides for formatted input conversion;
it will read the standard input and break it
up into strings, numbers, etc.,
uses the same mechanism as
so calls to them may also be intermixed.
read only one input and write one output;
may be entirely adequate,
and it is almost always enough to get started.
This is particularly true if
pipe facility is used to connect the output of
one program to the input of the next.
For example, the following program
strips out all ascii control characters
(except for newline and tab).
main() /* ccstrip: strip non-graphic characters */
while ((c = getchar()) != EOF)
if ((c >= ' ' && c < 0177) || c == '\t' || c == '\n')
should appear at the beginning of each source file.
It causes the C compiler to read a file
.IT /usr/include/stdio.h ) (
standard routines and symbols
that includes the definition of
If it is necessary to treat multiple files,
to collect the files for you:
cat file1 file2 ... | ccstrip >output
and thus avoid learning how to access files from a program.
at the end is not necessary to make the program work
but it assures that any caller
of the program will see a normal termination status
from the program when it completes.
Section 6 discusses status returns in more detail.