생산자

Yoo Moon Il

토픽 125 / 봤어요 51

25 [C] 변수와 범위

2018-05-17 14:59:57

변수와 범위

외부 변수

A C program consists of a set of external objects, which are either variables or functions. The adjective “external” is used in contrast to “internal,” which describes the arguments and variables defined inside functions. External variables are defined outside of any function, and are thus potentially available to many functions. Functions themselves are always external, because C does not allow functions to be defined inside other functions. By default, external variables and functions have the property that all references to them by the same name, even from functions compiled separately, are references to the same thing. (The standard calls this property external linkage.)

[The C Programming Language p.73]

자동 지역 변수 (automatic variable)

Automatic variables are internal to a function; they come into existence when the function is entered, and disappear when it is left. External variables, on the other hand, are permanent, so they retain values from one function invocation to the next.

[The C Programming Language p.74]

변수의 범위

The scope of a name is the part of the program within which the name can be used. For an automatic variable declared at the beginning of a function, the scope is the function in which the name is declared. Local variables of the same name in different functions are unrelated. The same is true of the parameters of the function, which are in effect local variables. The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled. For example, if main, sp, val, push, and pop are defined in one file, in the order shown above, that is,

main() { ... }

int sp =0;

double val[MAXVAL];

void push( double f) { ... }

doublepop(void) { … }

then the variables sp and val may be used in push and pop simply by naming them; no further declarations are needed. But these names are not visible in main, nor are push and pop themselves.

On the other hand, if an external variable is to be referred to before it is defined, or if it is defined in a different source file from the one where it is being used, then an extern declaration is mandatory.

extern 지정자의 선언과 정의

It is important to distinguish between the declarationof an external variable and its definition. A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside. If the lines

int sp;

double val[MAXVAL];

appear outside of any function, they define the external variables sp and val, cause storage to be set aside, and also serve as the declaration for the rest of that source file. On the other hand, the lines

extern int sp;

extern double val[ ];

declarefor the rest of the source file that sp is an int and that val is a double array (whose size is determined elsewhere), but they do not create the variables or reserve storage for them.

There must be only one definitionof an external variable among all the files that make up the source program; other files may contain extern declarations to access it. (There may also be extern declarations in the file containing the definition.) Array sizes must be specified with the definition, but are optional with an extern declaration.

Initialization of an external variable goes only with the definition.

Although it is not a likely organization for this program, the functions push and pop could be defined in one file, and the variables val and sp defined and initialized in another. Then these definitions and declarations would be necessary to tie them together:

In file 1:

externint sp;

externdouble val[ ];

void push(double f) { … }

doublepop(void) { … }

In file2:

int sp =0;

double val[MAXVAL];

Because the extern declarations in file 1 lie ahead of and outside the function definitions, they apply to all functions; one set of declarations suffices for all of

file 1. This same organization would also be needed if the definitions of sp and val followed their use in one file.

[The C Programming Language p.80-81]

정적 변수

The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled.

Static storage is specified by prefixing the normal declaration with the word static. If the two routines and the two variables are compiled in one file, as in

then no other routine will be able to access buf and bufp, and those names will not conflict with the same names in other files of the same program.

The external static declaration is most often used for variables, but it can be applied to functions as well. Normally, function names are global, visible to any part of the entire program. If a function is declared static, however, its name is invisible outside of the file in which it is declared.

The static declaration can also be applied to internal variables. Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. This means that internal static variables provide private, permanent storage within a single function.