4.5. Summary
With the appropriate declarations, you can have names that are visible
throughout the program or limited to a single file or limited to a single
function, as appropriate.
Here are the combinations of the use of the keywords, the types of
declarations and the resulting linkage:
Declaration |
Keyword |
Resulting Linkage |
Accessibility |
Note |
external |
none |
external |
entire program |
2 |
external |
extern |
external |
entire program |
2 |
external |
static |
internal |
a single file |
2 |
internal |
none |
none |
a single function |
|
internal |
extern |
external |
entire program |
1 |
internal |
static |
none |
a single function |
2 |
Although the accessibility of internal declarations prefixed with
extern is program-wide, watch out for the scope of the
name. | External (or internal static ) objects are initialized
once only, at program start-up. The absence of explicit initialization
is taken to be a default initialization of zero. | Table 4.2. Summary of Linkage
There are a few golden rules for the use of functions that are worth
re-stating too.
- To use a function returning other than
int , a
declaration or definition must be in scope.
- Do not return from a function by falling out of its body unless its
type is
void .
A declaration of the types of arguments that a function takes is not
mandatory, but it is extremely strongly recommended.
Functions taking a variable number of arguments can be written portably
if you use the methods described in Section 9.9.
Functions are the cornerstone of C. Of all the changes to the language,
the Standard has had by far its most obvious effect by introducing
function prototypes. This change has won widespread approval throughout
the user community and should help to produce a substantial improvement
in reliability of C programs, as well as opening the possibility of
optimization by compilers in areas previously closed to them.
The use of call-by-value is sometimes surprising to people who have
used languages that prefer a different mechanism, but at least the C
approach is the ‘safest’ most of the time.
The attempts by the Standard to remove ambiguity in the scope and
meaning of declarations are interesting, but frankly have explored an
obscure region which rarely caused any difficulties in practice.
From the beginner's point of view, it is important to learn thoroughly
everything discussed in this chapter, perhaps with the exception of the
linkage rules. They can be deferred for a more leisurely inspection at
some later time.
Footnotes
|
Printer-friendly version
The C Book
This book is published as a matter of historical interest.
Please read the
copyright and disclaimer information.
GBdirect Ltd provides up-to-date training and consultancy in
C,
Embedded C,
C++
and a wide range of
other subjects based on
open standards if you happen to be interested.
|