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