🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2202

Re: "extern" and function declarations

#2202
From: Guillaume Laurent
Date: Thu, 07 Mar 1996 14:43
49 lines
1885 bytes
In article <4hdqjq$8pr@solutions.solon.com> fred@genesis.demon.co.uk (Lawrence Kirby) writes:


> I mentioned object declarations above. Consider the following (assume
> everything from here on is declared outside of any function):
> 
>     int i = 1;
> 
> The simplest case - this is a declaration that reserves space and hence
> is also a definition. It has file scope i.e. is visible up until the end
[...]
>
>     extern int i = 1;
> 
> This means exactly the same thing as int i = 1; The initialisation 'forces'
> the definition (allocation of space).

I've got a question here : what if the same above declaration is made
in two (or more) files, compiled seperately then linked together ?

ie, in file a.c :

extern int i = 1;

...

and in file b.c :

extern int i = 2;

If I understand correctly what you've said, both of these declarations
will reserve space, and give i a value. My question is, what decides
which value will actually be "used", and what happens to the "duplicate"
space ?

>From what I've tried, the final effective value of i depends on the
order of linking of the objects :

cc a.o b.o -o foo  ---> i=1
cc b.o a.o -o foo  ---> i=2

But I'm still wondering about the "duplicate" space...

> I'm curious - do people find this enlightening (you learnt something),

Yes, I do.
-- 
--
					Guillaume
Technical Details
Message-ID: <4hmslm$b3l@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4hdqjq$8pr@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Guillaume Laurent <glaurent@nissart.lagaude.ibm.com>
Newsgroups: comp.lang.c.moderated
Subject: Re: "extern" and function declarations
Date: 7 Mar 1996 08:43:02 -0600
Organization: IBM La Gaude Research Center
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hmslm$b3l@solutions.solon.com>
References: <4hdqjq$8pr@solutions.solon.com>
Reply-To: glaurent@worldnet.net
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Gnus v5.0.4