Re: Help wanted on some source codes



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
.



Relevant Pages

  • Re: Diagnostic required for missing function definition?
    ... you'll get a linker error, ... exactly one external definition for the identifier; ... void read; ... int main ...
    (comp.lang.c)
  • Re: Help wanted on some source codes
    ... > void main ... also has maincall a function without a declaration in scope. ... If you are using gcc as a standard compiler conforming to the C ... there shall be exactly one external definition for the identifier; ...
    (comp.lang.c)