include problem
[unix-history] / .ref-BSD-3 / usr / doc / uprog / p2
CommitLineData
8340f87c
BJ
1.NH
2BASICS
3.NH 2
4Program Arguments
5.PP
6When a C program is run as a command,
7the arguments on the command line are made available
8to the
9function
10.UL main
11as an argument count
12.UL argc
13and an array
14.UL argv
15of
16pointers to
17character strings
18that contain
19the arguments.
20By convention,
21.UL argv[0]
22is the command name itself,
23so
24.UL argc
25is always greater than 0.
26.PP
27The following program illustrates the mechanism:
28it simply echoes its arguments
29back to the terminal.
30(This is essentially the
31.UL echo
32command.)
33.P1
34main(argc, argv) /* echo arguments */
35int argc;
36char *argv[];
37{
38 int i;
39
40 for (i = 1; i < argc; i++)
41 printf("%s%c", argv[i], (i<argc-1) ? ' ' : '\n');
42}
43.P2
44.UL argv
45is a pointer to an array
46whose individual elements are pointers to arrays of characters;
47each is terminated by
48.UL \e0 ,
49so they can be treated as strings.
50The program starts by printing
51.UL argv[1]
52and loops until it has printed them all.
53.PP
54The argument count and the arguments
55are parameters to
56.UL main .
57If you want to keep them around so other
58routines can get at them, you must
59copy them to external variables.
60.NH 2
61The ``Standard Input'' and ``Standard Output''
62.PP
63The simplest input mechanism is to read the ``standard input,''
64which is generally the user's terminal.
65The function
66.UL getchar
67returns the next input character each time it is called.
68A file may be substituted for the terminal by
69using the
70.UL <
71convention:
72if
73.UL prog
74uses
75.UL getchar ,
76then
77the command line
78.P1
79prog <file
80.P2
81causes
82.UL prog
83to read
84.UL file
85instead of the terminal.
86.UL prog
87itself need know nothing about where its input
88is coming from.
89This is also true if the input comes from another program via
90the
91.U
92pipe mechanism:
93.P1
94otherprog | prog
95.P2
96provides the standard input for
97.UL prog
98from the standard output of
99.UL otherprog.
100.PP
101.UL getchar
102returns the value
103.UL EOF
104when it encounters the end of file
105(or an error)
106on whatever you are reading.
107The value of
108.UL EOF
109is normally defined to be
110.UL -1 ,
111but it is unwise to take any advantage
112of that knowledge.
113As will become clear shortly,
114this value is automatically defined for you when
115you compile a program,
116and need not be of any concern.
117.PP
118Similarly,
119.UL putchar(c)
120puts the character
121.UL c
122on the ``standard output,''
123which is also by default the terminal.
124The output can be captured on a file
125by using
126.UL > :
127if
128.UL prog
129uses
130.UL putchar ,
131.P1
132prog >outfile
133.P2
134writes the standard output on
135.UL outfile
136instead of the terminal.
137.UL outfile
138is created if it doesn't exist;
139if it already exists, its previous contents are overwritten.
140And a pipe can be used:
141.P1
142prog | otherprog
143.P2
144puts the standard output of
145.UL prog
146into the standard input of
147.UL otherprog.
148.PP
149The function
150.UL printf ,
151which formats output in various ways,
152uses
153the same mechanism as
154.UL putchar
155does,
156so calls to
157.UL printf
158and
159.UL putchar
160may be intermixed in any order;
161the output will appear in the order of the calls.
162.PP
163Similarly, the function
164.UL scanf
165provides for formatted input conversion;
166it will read the standard input and break it
167up into strings, numbers, etc.,
168as desired.
169.UL scanf
170uses the same mechanism as
171.UL getchar ,
172so calls to them may also be intermixed.
173.PP
174Many programs
175read only one input and write one output;
176for such programs I/O
177with
178.UL getchar ,
179.UL putchar ,
180.UL scanf ,
181and
182.UL printf
183may be entirely adequate,
184and it is almost always enough to get started.
185This is particularly true if
186the
187.UC UNIX
188pipe facility is used to connect the output of
189one program to the input of the next.
190For example, the following program
191strips out all ascii control characters
192from its input
193(except for newline and tab).
194.P1
195#include <stdio.h>
196
197main() /* ccstrip: strip non-graphic characters */
198{
199 int c;
200 while ((c = getchar()) != EOF)
201 if ((c >= ' ' && c < 0177) || c == '\t' || c == '\n')
202 putchar(c);
203 exit(0);
204}
205.P2
206The line
207.P1
208#include <stdio.h>
209.P2
210should appear at the beginning of each source file.
211It causes the C compiler to read a file
212.IT /usr/include/stdio.h ) (
213of
214standard routines and symbols
215that includes the definition of
216.UL EOF .
217.PP
218If it is necessary to treat multiple files,
219you can use
220.UL cat
221to collect the files for you:
222.P1
223cat file1 file2 ... | ccstrip >output
224.P2
225and thus avoid learning how to access files from a program.
226By the way,
227the call to
228.UL exit
229at the end is not necessary to make the program work
230properly,
231but it assures that any caller
232of the program will see a normal termination status
233(conventionally 0)
234from the program when it completes.
235Section 6 discusses status returns in more detail.