Newsgroup: comp.lang.c


Date: Mon, 17 Apr 2006 17:50:56 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0
MIME-Version: 1.0
Newsgroups: comp.lang.c
Subject: Re: Doubts about Linkage Rules
References: <44439e56$0$36934$4fafbaef@reader3.news.tin.it>
In-Reply-To: <44439e56$0$36934$4fafbaef@reader3.news.tin.it>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
fctk wrote:
> source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2
> 
> there are two passages in this paragraph i can't fully understand:
> 
> 1) "If the declaration of an identifier for an object or a function 
> contains the storage-class specifier extern , the identifier has the 
> same linkage as any visible declaration of the identifier with file 
> scope. If there is no visible declaration with file scope, the 
> identifier has external linkage."
> 
> in particular: "the identifier has the same linkage as /any/ /visible/ 
> declaration of the identifier with file scope"

It means that the following is a legal declaration of foo with file 
linkage scope:

static int foo;
extern int foo;

and this a legal declaration of bar with external linkage scope:

int bar;
extern int bar;

The rationale document for C89 explains how this requirement allows the 
implementation of a one-pass compiler that generates intermediate 
assembly code.

Note that C99 clarifies the *visible* part of the specification as 
follows (6.2.2):

"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."

> 2) "If, within a translation unit, the same identifier appears with both 
> internal and external linkage, the behavior is undefined."
> 
> i can't imagine an example of an identifier having both internal and 
> external linkage...

This is not allowed:

static int bar;
int bar;

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality



Newsgroup comp.lang.c contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.