By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Puzzle about multi definitions

P: n/a
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)
{
}

Mar 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)."
And is the main() function very special?
Yes, 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)
{

}

Mar 21 '07 #2

P: n/a
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
Mar 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.