On 20 Mar 2007 19:27:05 -0700, "user923005" <dc*****@connx.comwrote
in comp.lang.c:
On Mar 20, 7:13 pm, "erichain" <erich...@gmail.comwrote:
If I define global variables with the same name in different files,
what will the linker say according to the C standard(s)?
The C standard does not define a linker, let alone what a linker might
say. It does, however, define linkage.
And can variable and function have the same name?
Under the appendix listing causes of undefined behavior, we find this:
"J.5.11 Multiple external definitions
1 There may be more than one external definition for the identifier of
an object, with or
without the explicit use of the keyword extern; if the definitions
disagree, or more than
one is initialized, the behavior is undefined (6.9.2)."
This is not really a good answer, Appendix J is informative, and not
normative, and section 5 of Appendix J is titled "Common Extensions".
What the actual normative text of the standard says, 6.9 p5, is:
"An external definition is an external declaration that is also a
definition of a function (other than an inline definition) or an
object. If an identifier declared with external linkage is used in an
expression (other than as part of the operand of a sizeof operator
whose result is an integer constant), somewhere in the entire program
there shall be exactly one external definition for the identifier;
otherwise, there shall be no more than one."
So the fact that the standard recognizes that some implementations
allow for some situations of this type does not change the fact that
it is undefined for violating a "shall" outside of a constraint
section.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html