Use of EXTERN

T

Tom Young

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?
 
J

James Kuyper

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.
 

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


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top