Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #7323 > unrolled thread

Use of EXTERN

Started byTom Young <tyoung37@gmail.invalid>
First post2011-06-30 20:38 +0000
Last post2011-07-01 07:11 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c


Contents

  Use of EXTERN Tom Young <tyoung37@gmail.invalid> - 2011-06-30 20:38 +0000
    Re: Use of EXTERN pete <pfiland@mindspring.com> - 2011-06-30 17:37 -0400
    Re: Use of EXTERN James Kuyper <jameskuyper@verizon.net> - 2011-07-01 07:11 -0400

#7323 — Use of EXTERN

FromTom Young <tyoung37@gmail.invalid>
Date2011-06-30 20:38 +0000
SubjectUse of EXTERN
Message-ID<iuimrb$9c$1@speranza.aioe.org>
Hi all.  I've read through all the K&R2 chapters and I'm now reading the 
appendices.
Specifically, I'm reading section A11 - Scope & Linkage p227.  I have a
question about the "extern" property.

Section A11.2 Linkage p228 says:

"All declarations for the same object or function identifier with external
linkage refer to the same thing, and the object or function is shared by
the entire program."

and

"... the first external declaration for an identifier gives the identifier
internal linkage if the static specifier is used, external linkage
otherwise."

My questions are, is there any point to declaration an external object
"extern"?  Does it have any effect(s)?  I'm almost positive I've seen code
with external declarations that included the keyword "extern", but given
the quoted statements above, that seems redundant.  Am I missing
something?

[toc] | [next] | [standalone]


#7327

Frompete <pfiland@mindspring.com>
Date2011-06-30 17:37 -0400
Message-ID<4E0CECB1.3D9D@mindspring.com>
In reply to#7323
Tom Young wrote:
> 
> Hi all.  I've read through all the
> K&R2 chapters and I'm now reading the appendices.
> Specifically, I'm reading section A11 - Scope & Linkage p227. 
> I have a question about the "extern" property.
> 
> Section A11.2 Linkage p228 says:
> 
> "All declarations for the same object or function identifier
> with external linkage refer to the same thing,
> and the object or function is shared by
> the entire program."
> 
> and
> 
> "... the first external declaration for an identifier
> gives the identifier
> internal linkage if the static specifier is used,
> external linkage otherwise."
> 
> My questions are,
> is there any point to declaration an external object "extern"?
> Does it have any effect(s)?
> I'm almost positive I've seen code
> with external declarations that included the keyword "extern",
> but given
> the quoted statements above, that seems redundant.  Am I missing
> something?

I have a program called "e_driver".

It consists of six files:
 http://www.mindspring.com/~pfilandr/C/e_driver/e_driver.c
 http://www.mindspring.com/~pfilandr/C/e_driver/e_driver.h
 http://www.mindspring.com/~pfilandr/C/e_driver/e_sort.c
 http://www.mindspring.com/~pfilandr/C/e_driver/e_sort.h
 http://www.mindspring.com/~pfilandr/C/e_driver/e_distributions.c
 http://www.mindspring.com/~pfilandr/C/e_driver/e_distributions.h

e_driver.c has this external declaration:
 long unsigned cmp_count;

e_driver.h has this external declaration:
 extern long unsigned cmp_count;

e_driver.c and e_sort.h and e_distributions.h
all have this line:
 #include "e_driver.h"

e_sort.c has this line:
 #include "e_sort.h
and e_distributions.c has this line:
 #include "e_distributions.h"

All three of the *.c files in the e_driver program
use the same external object (cmp_count),
which is declared in e_driver.c

-- 
pete

[toc] | [prev] | [next] | [standalone]


#7353

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-07-01 07:11 -0400
Message-ID<iuka0u$kpp$1@dont-email.me>
In reply to#7323
On 06/30/2011 04:38 PM, Tom Young wrote:
> Hi all.  I've read through all the K&R2 chapters and I'm now reading the 
> appendices.
> Specifically, I'm reading section A11 - Scope & Linkage p227.  I have a
> question about the "extern" property.
> 
> Section A11.2 Linkage p228 says:
> 
> "All declarations for the same object or function identifier with external
> linkage refer to the same thing, and the object or function is shared by
> the entire program."
> 
> and
> 
> "... the first external declaration for an identifier gives the identifier
> internal linkage if the static specifier is used, external linkage
> otherwise."
> 
> My questions are, is there any point to declaration an external object
> "extern"?  Does it have any effect(s)?  I'm almost positive I've seen code
> with external declarations that included the keyword "extern", but given
> the quoted statements above, that seems redundant.  Am I missing
> something?

The problem is that, unless you use the keyword 'extern', the
declaration of an object will be treated as a tentative definition. If
no actual definition is present in the rest of the translation unit, a
tentative definition is automatically converted into an actual
definition, implicitly zero-initialized. (6.9.2p2)

C allows only one external definition of an identifier in an entire
program (6.9p3), so you can only allow the declaration to become a
definition in one translation unit. In all other translation units, you
must use the 'extern' keyword to keep it from converting into a definition.
-- 
James Kuyper

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web