Puzzle about multi definitions

E

erichain

If I define global variables with the same name in different files,
what will the linker say according to the C standard(s)?
And can variable and function have the same name?
And is the main() function very special?
Thanks in advance!

e.g.

/* file1.c */

int a;
int b = 0;
int c = 1;
int d;
int e = 5;
int f = 6;
int func1;
int func2;

int f(void)
{
return 1;
}

int main(void)
{
f;
f();
func1;
func1();
func2;
func2();
return 0;
}

/* file2.c */

int a;
int b = 0;
int c = 2;
double d;
double e = 5.0;
double f = 6.6;
int main;

int func1(void)
{
return 2;
}

void func2(void)
{
}
 
U

user923005

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)."
And is the main() function very special?

Yes, very special.
 
J

Jack Klein

The C standard does not define a linker, let alone what a linker might
say. It does, however, define linkage.


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
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top