I am intermediate level C programmer. I was walking through a simple code snippet in C
int a ; /开发者_JAVA技巧/ A
const int b; // B
static int c; //C
void func(int d) // D
{
//.....
}
What are the linkage of variables a,b,c and d. I am quite sure that a
by default has external linkage, b
, c
and d
have internal linkage. Is my understanding correct?
This is my first question at this site.
From section 6.2.2, Linkages of identifiers
, of C99, the definitive source:
1/ An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.
2/ In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.
3/ If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.
4/ For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
5/ If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
6/ The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
7/ If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
Now, tackling your variables one by one:
a
is covered by part 5 since it's "an identifier for an object [that] has file scope and no storage-class specifier". It therefore has external linkage.b
is also covered by part 5 (file scope, no storage-class specifier). Hence external linkage.c
is covered by part 3 since it has thestatic
storage-class specifier - it has internal linkage.- finally,
d
is covered by part 6, being a function parameter - it has no linkage.
In C, a
and b
have external linkage and c
has internal linkage. In C++ you'd be right,that b
would also have internal linkage.
For case "D", I'm not sure which you're talking about: the function or the parameter. func
has external linkage; d
has no linkage.
I am quite sure that a by default has external linkage,
b
,c
andd
have internal linkage. Is my understanding correct?
No! d
has no-linkage because it is a formal parameter.
From C99
The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
Furthermore b
has external linkage in C99. In C++ const-qualified variables at file scope have internal linkage.
a,b and c all have identical linkage, if they are declared in global scope which it looks like they are
d is a function parameter, and is created when that function is called and no longer exists when the function returns
精彩评论