extern
is a storage class specifier. This is just a fact of the language grammar. extern
has a number of effects on the semantics of a program depending on where it is used. It doesn't have the single same effect everywhere. It influences the storage duration and linkage of objects and it also helps determine whether some declarations are also definitions or not.
E.g.:
int a; // Ex1
extern int b; // Ex2
For example, if Ex1
and Ex2
where at global scope then they would both refer to objects with static storage duration and external linkage. In C++, though, the first would be a definition (tentative definition in C) and the second would not. In this example extern
has not changed the storage duration or linkage of the declared object.
If Ex1
and Ex2
occurred in a function body then a
would refer to an object with automatic storage duration and no linkage but b
would refer to an object with external linkage and static storage duration. In this example, extern
has affected the meaning of the declaration in both linkage, storage duration and whether or not it is a definition.
Finally, in C++, here is an example where the only effect of extern
is changing the linkage from internal to external.
const int c = 5; // static storage duration, internal linkage
extern const int d = 10; // static storage duration, external linkage
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…