extern and linkage

S

Steve Kobes

Consider

static int x;
void f()
{
extern int x; /* internal or external linkage? */
}

C99 6.2.2.4 says that "extern" causes an identifier's declaration to
inherit a previously specified linkage when it occurs "in a scope in
which a prior declaration of that identifier is visible". One might
think this means the inner x has internal linkage. Visual C++ 6 seems
to think so too.

But the footnote immediately afterward says, "As specified in 6.2.1
[scopes], the later declaration might hide the prior declaration." If
this footnote is to make sense, the inner x would have external
linkage, because its declaration hides that of the outer x. If so, it
is a bug in VC++ (which of course would not be all that surprising).

Which interpretation is correct?
 
C

Chris Torek

Consider

static int x;
void f()
{
extern int x; /* internal or external linkage? */
}

C99 6.2.2.4 says that "extern" causes an identifier's declaration to
inherit a previously specified linkage when it occurs "in a scope in
which a prior declaration of that identifier is visible". One might
think this means the inner x has internal linkage. Visual C++ 6 seems
to think so too.

I believe this is correct.
But the footnote immediately afterward says, "As specified in 6.2.1
[scopes], the later declaration might hide the prior declaration." If
this footnote is to make sense, the inner x would have external
linkage, because its declaration hides that of the outer x. If so, it
is a bug in VC++ (which of course would not be all that surprising).

I believe this footnote refers instead to code such as this:

static int x;
void f(void) {
auto int x; /* hides "static" one; "auto" keyword for ref below */
{
extern int x;
...
}
}

Here the block-scope "auto" x has made the file-scope "x", with its
static duration, invisible. The "extern" line can no longer see it.

Note, however, that comp.lang.c is not the best group for questions
of the form: "what do these particular words in this particular
paragraph on this particular page of the C standard mean?" Along
with proposals for future standards, questions about interpretation
of current (and even past) standards is the domain of comp.std.c.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top