📄 ct7
字号:
.NH Scope Rules: Who Knows About What.PPA complete C program need not be compiled all at once;the source text of the program may be kept in several files,and previously compiled routines may be loaded from libraries.How do we arrange that data gets passed from oneroutine to another?We have already seen how to use function arguments and values,so let us talk about external data.Warning: the words.uldeclarationand.uldefinitionare used precisely in this section;don't treat them as the same thing..PPA major shortcut exists for making .UL externdeclarations.If the definition of a variable appears.ulbeforeits use in some function,no.UL externdeclaration is needed within the function.Thus, if a file contains.E1f1( ) { \*.\*.\*. }.SPint foo;.SPf2( ) { \*.\*.\*. foo = 1; \*.\*.\*. }.SPf3( ) { \*.\*.\*. if ( foo ) \*.\*.\*. }.E2no declaration of.UL foois needed in either.UL f2or or.UL f3,because the external definition of.UL fooappears before them.But if.UL f1wants to use.UL foo,it has to contain the declaration.E1f1( ) { extern int foo; \*.\*.\*.}.E2.PPThis is true also of any function that existson another file _if it wants.UL fooit has to use an.UL externdeclarationfor it.(If somewhere there is an.UL externdeclaration for something,there must also eventually be an external definition of it,or you'll get an ``undefined symbol'' message.).PPThere are some hidden pitfalls in external declarationsand definitions if you use multiple source files.To avoid them, first,define and initialize each external variable only once in the entire set of files:.E1int foo 0;.E2You can get away with multiple external definitions on .UC UNIX,but not on.UC GCOS,so don't ask for trouble.Multiple initializations are illegal everywhere.Second,at the beginning of any file that contains functions needing a variablewhose definition is in some other file,put in an.UL externdeclaration,outside of any function:.E1extern int foo;.SPf1( ) { \*.\*.\*. } etc\*..E2.PPThe .UL #includecompiler control line,to be discussed shortly,lets you make a single copy of the external declarationsfor a program and then stick them into each of the source filesmaking up the program..NH#define, #include.PPC provides a very limited macro facility.You can say.E1#define name something.E2and thereafter anywhere ``name'' appears as a token,``something'' will be substituted.This is particularly useful in parametering the sizes of arrays:.E1#define ARRAYSIZE 100 int arr[ARRAYSIZE]; \*.\*.\*. while( i\*+ < ARRAYSIZE )\*.\*.\*..E2(now we can alter the entire program by changing only the.UL define)or in setting up mysterious constants:.E1#define SET 01#define INTERRUPT 02 /\** interrupt bit \**/#define ENABLED 04 \*.\*.\*.if( x & (SET | INTERRUPT | ENABLED) ) \*.\*.\*..E2Now we have meaningful words instead of mysterious constants.(The mysterious operators `&' (AND)and `\(or' (OR)will be covered in the next section.)It's an excellent practice to write programswithout any literal constants except in.UL #definestatements..PPThere are several warnings about.UL #define\*.First, there's no semicolon at the end of a.UL #define;all the text from the name to the end of the line(except for comments)is taken to be the ``something''.When it's put into the text, blanks are placed around it.Good style typically makes the name in the .UL #defineupper case _this makes parameters more visible.Definitions affect things only after they occur,and only within the file in which they occur.Defines can't be nested.Last, if there is a.UL #definein a file,then the first character of the file .ulmustbe a `#',to signal the preprocessor that definitions exist..WS.PPThe other control word known to C is.UL #include\*.To include one file in your source at compilation time, say.E1#include "filename".E2This is useful for putting a lot of heavily used data definitions and .UL #definestatements at the beginning of a file to be compiled.As with .UL #define,the first line of a file containing a.UL #includehas to begin with a `#'.And.UL #includecan't be nested _an included file can't contain another.UL #include\*.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -