extern question

J

Josh Lessard

Hi all. I've read through all the K&R2 chapters (thanks for suggesting
it...it was a great read) and I'm now reading the appendices.
Specifically, I'm reading section A11 - Scope & Linkage p227. I have a
question about the "extern" specifier.

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?

*****************************************************
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*****************************************************
 
M

Matt

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?

Yes (I'll let you decide which question I'm answering ;-) ), extern
comes in handy when you have multiple .c or .cpp files that are linked
together. If you declare it in one it will not be known of in the
other, and if you declare it in a common .h file, it will have multiple
definitions (be declared twice and the compiler will scream bloody
murder). So if you put extern in the common header (or in the .c or .cpp
file(s) that you didn't declare it in, and declare your variable in only
on of your .c or .cpp files, then the compiler is happy and you can
access your variable from everywhere in your program. Please note,
however that using extern might possibly get you martyred as it is used
for global variables (AFAIK), and to some people, these hold religious
significance as the bane of all that is holy and good programming
practice. I must say, however that there are some places where they are
handy and some that they are necessary (or at least helpful). I won't
go any further, lest I start a holy war. Hope I was of help, but I
didn't see anyone respond to your question, and I hope your still
checking out the newsgroups as it has been quite a while. You've
probably already figured out the answer one way or another, but such an
unanswered question bothers me, so let's just say I did it for my peace
of mind ;-)
-Matt
 

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

Similar Threads

Use of EXTERN 1
composite type example from C11 standard 2
Specifier extern 5
Pointer to extern char? 3
extern "C" friend function without external linkage 2
extern and linkage 1
Doubts about Linkage Rules 13
asm 15

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top