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