Scope & Linkage

A translation unit is a file of source code that the C compiler can sucessfully process.  It may contain only declarations, only function definitions, or both.  Therefore, it need not include a main function.  For a function definition, the file must contain the entire source code of the function between curley braces; and also for each object referenced therein, a declaration occurring at a point, either inside the function or outside of any function, that is before the object’s first reference.

This characteristic of the C compiler makes it possible to divide the source code of a C program among multiple source files, all of which must be translation units.  Each file is compiled alone, then all of the object files are linked together.  Only one of the files may include the main function, and this file’s name is given to the resulting executable file.  This practice, termed modular programming, provides the ability to store a library of precompiled object files, one for each commonly-used function or group of related functions, to be linked into many future programs, like standard parts.  Therefore, for a new program, the new code that is required may be limited to the main function and other application-specific functions.

An object is a location in storage, and functions defined in separate source files may reference the same one.  The conventions by which the C compiler understands an object’s name in multiple contexts are scope and linkage.

A declaration states the nature of an object or function.  The declaration of an object or function is termed an internal declaration when it occurs at the beginning of a block within a function; or an external declaration when it occurs outside of any function.

A definition is a declaration that allocates storage.  An internal variable is an object whose definition is an internal declaration, and it may include an initializer that is a constant or variable expression.  An external variable is an object whose definition is an external declaration, and it may include an initializer that is a constant expression.

For a function, a function prototype is a declaration, but not a definition; the source code of a function is a definition.  For an object, a declaration is the definition, depending upon it’s context and the specification of several keywords.

The extern keyword in the declaration of an object indicates that storage is allocated elsewhere, and that the declaration is not a definition.  The exception to this rule, is an external declaration with an initializer, as described below.

An internal declaration of an object that omits the extern keyword is the definition of an internal variable.  Only one definition of the same internal variable may be present within the same block.  An external declaration of an object that includes an initializer is the definition of an external variable, even if it includes the extern keyword.  If it omits both an initializer and the extern keyword, it is a tentative definition.  The compiler regards all tentative definitions that are in the same source file as the definition of an external variable of the same name as redundant declarations.  If only tentative definitions of the same object name are present in the source file, the compiler combines them into a single definition with an initializer of 0.  Only one definition of the same external variable may be present in the entire program.

The lexical scope of a declaration is the declared name’s range of visibility within its own translation unit.  It begins at the end of the declaration and continues unbroken until an end point that depends on the type of declaration.  For an internal declaration, that point is the end of the containing block.  For a parameter of a function definition, that point is the end of the function.  For an external declaration, that point is the end of the translation unit.  Nested declarations of the same name have overlapping lexical scopes, and each one hides the others that are outside its own block.  A declaration of an object, including the extern keyword, which occurs earlier than the object’s definition in the same source file, commences the lexical scope at the earlier point.

All function definitions are external, since C does not allow nested functions.  Therefore, a translation unit consists of external declarations (declarations of objects and functions) and function definitions.

An object or function has a scope with respect to the entire program which is determined by: its storage class, the lifetime of the storage associated with it; and its linkage, its connection to other declarations of the same name.  In all the files that together comprise a C program, each function definition or external variable must specify a unique name.

There are two storage classes that apply to objects, automatic and static.  Storage of an automatic variable has the lifetime of the closest enclosing block.  Storage of a static variable has the lifetime of the program.  An internal variable is automatic, unless its definition includes the static keyword, in which case it is static, and its value persists between function calls.  An external variable is always static.

Linkage is the property of a declaration that determines its connection to other declarations of the same name across the entire program.  Internal linkage connects a declaration to only the other declarations in the same source file.  External linkage connects a declaration to other declarations having external linkage in other source files.

An object or function having internal linkage is said to have local scope; its visibility is resticted to its own source file.  An internal declaration that omits the extern keyword has no linkage beyond its enclosing block.  An external declaration that includes the static keyword is restricted to internal linkage.  By this means, an external variable or function definition is hidden from other source files.

An object or function having external linkage is said to have global scope; its visibility consists of the combined lexical scopes of all its connected declarations.  An internal declaration that includes the extern keyword has external linkage.  An external declaration that omits the static keyword has external linkage.


Copyright © 2003 The Stevens Computing Services Company, Inc.  All rights reserved.