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