Commit | Line | Data |
---|---|---|
b3f974bf BK |
1 | .NH |
2 | Scope Rules: Who Knows About What | |
3 | .PP | |
4 | A complete C program need not be compiled all at once; | |
5 | the source text of the program may be kept in several files, | |
6 | and previously compiled routines may be loaded from libraries. | |
7 | How do we arrange that data gets passed from one | |
8 | routine to another? | |
9 | We have already seen how to use function arguments and values, | |
10 | so let us talk about external data. | |
11 | Warning: the words | |
12 | .ul | |
13 | declaration | |
14 | and | |
15 | .ul | |
16 | definition | |
17 | are used precisely in this section; | |
18 | don't treat them as the same thing. | |
19 | .PP | |
20 | A major shortcut exists for making | |
21 | .UL extern | |
22 | declarations. | |
23 | If the definition of a variable appears | |
24 | .ul | |
25 | before | |
26 | its use in some function, | |
27 | no | |
28 | .UL extern | |
29 | declaration is needed within the function. | |
30 | Thus, if a file contains | |
31 | .E1 | |
32 | f1( ) { \*.\*.\*. } | |
33 | .SP | |
34 | int foo; | |
35 | .SP | |
36 | f2( ) { \*.\*.\*. foo = 1; \*.\*.\*. } | |
37 | .SP | |
38 | f3( ) { \*.\*.\*. if ( foo ) \*.\*.\*. } | |
39 | .E2 | |
40 | no declaration of | |
41 | .UL foo | |
42 | is needed in either | |
43 | .UL f2 | |
44 | or or | |
45 | .UL f3, | |
46 | because the external definition of | |
47 | .UL foo | |
48 | appears before them. | |
49 | But if | |
50 | .UL f1 | |
51 | wants to use | |
52 | .UL foo, | |
53 | it has to contain the declaration | |
54 | .E1 | |
55 | f1( ) { | |
56 | extern int foo; | |
57 | \*.\*.\*. | |
58 | } | |
59 | .E2 | |
60 | .PP | |
61 | This is true also of any function that exists | |
62 | on another file _ | |
63 | if it wants | |
64 | .UL foo | |
65 | it has to use an | |
66 | .UL extern | |
67 | declaration | |
68 | for it. | |
69 | (If somewhere there is an | |
70 | .UL extern | |
71 | declaration for something, | |
72 | there must also eventually be an external definition of it, | |
73 | or you'll get an ``undefined symbol'' message.) | |
74 | .PP | |
75 | There are some hidden pitfalls in external declarations | |
76 | and definitions if you use multiple source files. | |
77 | To avoid them, | |
78 | first, | |
79 | define and initialize each external variable only once in the entire set of files: | |
80 | .E1 | |
81 | int foo 0; | |
82 | .E2 | |
83 | You can get away with multiple external definitions on | |
84 | .UC UNIX, | |
85 | but not on | |
86 | .UC GCOS, | |
87 | so don't ask for trouble. | |
88 | Multiple initializations are illegal everywhere. | |
89 | Second, | |
90 | at the beginning of any file that contains functions needing a variable | |
91 | whose definition is in some other file, | |
92 | put in an | |
93 | .UL extern | |
94 | declaration, | |
95 | outside of any function: | |
96 | .E1 | |
97 | extern int foo; | |
98 | .SP | |
99 | f1( ) { \*.\*.\*. } | |
100 | etc\*. | |
101 | .E2 | |
102 | .PP | |
103 | The | |
104 | .UL #include | |
105 | compiler control line, | |
106 | to be discussed shortly, | |
107 | lets you make a single copy of the external declarations | |
108 | for a program and then stick them into each of the source files | |
109 | making up the program. | |
110 | .NH | |
111 | #define, #include | |
112 | .PP | |
113 | C provides a very limited macro facility. | |
114 | You can say | |
115 | .E1 | |
116 | #define name something | |
117 | .E2 | |
118 | and thereafter anywhere ``name'' appears as a token, | |
119 | ``something'' will be substituted. | |
120 | This is particularly useful in parametering the sizes of arrays: | |
121 | .E1 | |
122 | #define ARRAYSIZE 100 | |
123 | int arr[ARRAYSIZE]; | |
124 | \*.\*.\*. | |
125 | while( i\*+ < ARRAYSIZE )\*.\*.\*. | |
126 | .E2 | |
127 | (now we can alter the entire program by changing only the | |
128 | .UL define) | |
129 | or in setting up mysterious constants: | |
130 | .E1 | |
131 | #define SET 01 | |
132 | #define INTERRUPT 02 /\** interrupt bit \**/ | |
133 | #define ENABLED 04 | |
134 | \*.\*.\*. | |
135 | if( x & (SET | INTERRUPT | ENABLED) ) \*.\*.\*. | |
136 | .E2 | |
137 | Now we have meaningful words instead of mysterious constants. | |
138 | (The mysterious operators `&' (AND) | |
139 | and `\(or' (OR) | |
140 | will be covered in the next section.) | |
141 | It's an excellent practice to write programs | |
142 | without any literal constants except in | |
143 | .UL #define | |
144 | statements. | |
145 | .PP | |
146 | There are several warnings about | |
147 | .UL #define\*. | |
148 | First, there's no semicolon at the end of a | |
149 | .UL #define; | |
150 | all the text from the name to the end of the line | |
151 | (except for comments) | |
152 | is taken to be the ``something''. | |
153 | When it's put into the text, blanks are placed around it. | |
154 | Good style typically makes the name in the | |
155 | .UL #define | |
156 | upper case _ | |
157 | this makes parameters more visible. | |
158 | Definitions affect things only after they occur, | |
159 | and only within the file in which they occur. | |
160 | Defines can't be nested. | |
161 | Last, if there is a | |
162 | .UL #define | |
163 | in a file, | |
164 | then the first character of the file | |
165 | .ul | |
166 | must | |
167 | be a `#', | |
168 | to signal the preprocessor that definitions exist. | |
169 | .WS | |
170 | .PP | |
171 | The other control word known to C is | |
172 | .UL #include\*. | |
173 | To include one file in your source at compilation time, say | |
174 | .E1 | |
175 | #include "filename" | |
176 | .E2 | |
177 | This is useful for putting a lot of heavily used data definitions and | |
178 | .UL #define | |
179 | statements at the beginning of a file to be compiled. | |
180 | As with | |
181 | .UL #define, | |
182 | the first line of a file containing a | |
183 | .UL #include | |
184 | has to begin with a `#'. | |
185 | And | |
186 | .UL #include | |
187 | can't be nested _ | |
188 | an included file can't contain another | |
189 | .UL #include\*. |