473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Puzzle about multi definitions

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
2 1166
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: rossum | last post by:
Either I have found a puzzle, or I am just being stupid. Inside a namespace I have a class with a static const double member. The member is not an int so I cannot initialise it within the class,...
4
by: gdarian216 | last post by:
I am doing a multi file program and I got it to work correctly in a single file but when I split it up it isn't working properly. I keep getting errors when i compile. Can anyone help me figure out...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
4
by: Shug | last post by:
Hi, We're reformatting a lot of our project code using the excellent uncrustify beautifier. However, to gain confidence that it really is only changing whitespace (forget { } issues for just...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
198
by: Antoninus Twink | last post by:
Or, just how low has this group sunk? Twice in the past couple of days, Default Loser has accused Chris Hills, member of the ISO C committee, of being a troll (though given that the former's...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.