Re: Help wanted on some source codes
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 21:41:38 -0600
On 29 Nov 2005 18:34:15 -0800, "slebetman@xxxxxxxxx"
<slebetman@xxxxxxxxx> wrote in comp.lang.c:
> Old Wolf wrote:
> > Becker wrote:
> >
> > > 1.===========================================
> > > /* a.c */
> > > int x;
> > > int y;
> > >
> > > void main()
> > > {
> > > f();
> > > printf("%x %x\n", x, y);
> > > }
> > >
> > > /* b.c */
> > > double x;
> > >
> > > void f()
> > > {
> > > x = -0.0;
> > > }
> >
> > All of your programs have undefined behaviour because there
> > are two variables called 'x' with external linkage.
> >
>
> The programs are rubbish of course. But wouldn't the file scope of x
> mean that function f() is actually refering to the double x instead of
> the int x?
This is part of what I posted in a reply to the OP, but I'll repeat it
here for your benefit. The issue here is NOT file scope, but most
specifically IS external linkage:
The C standard says this in "6.9 External definitions" paragraph 5:
"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."
Violating a 'shall' outside of a constraint section is just plain old
ordinary undefined behavior. You have left planet C behind and
entered the Twilight Zone, and there is no right or wrong.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: Help wanted on some source codes
- From: slebetman@xxxxxxxxx
- Re: Help wanted on some source codes
- References:
- Help wanted on some source codes
- From: Becker
- Re: Help wanted on some source codes
- From: Old Wolf
- Re: Help wanted on some source codes
- From: slebetman@xxxxxxxxx
- Help wanted on some source codes
- Prev by Date: Re: Help wanted on some source codes
- Next by Date: Re: Help wanted on some source codes
- Previous by thread: Re: Help wanted on some source codes
- Next by thread: Re: Help wanted on some source codes
- Index(es):
Relevant Pages
|