Re: Help wanted on some source codes
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 21:39:26 -0600
On 29 Nov 2005 16:45:47 -0800, "Becker" <webmaster@xxxxxxxxx> wrote in
comp.lang.c:
> 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;
> }
[snip several other examples]
Each of your examples defines main() with a return type of void, and
also has main() call a function without a declaration in scope. There
is no version of the C standard where a program performing both of
these actions is legal.
If you are using gcc as a standard compiler conforming to the C
standard prior to 1999, 'void main()' invokes undefined behavior
immediately. Friends don't let friends void main(). If you are using
gcc as a standard compiler conforming to a 1999 or later version of
the C standard, it is a constraint violation to call a function
without a declaration in scope.
So you are not invoking gcc to conform to any version of the C
standard, which is just as well, because your program is not actually
valid C.
But as to the question you asked, even if you fixed these things:
Each of your examples defines 'x' with external linkage in two
different source files. It doesn't even make any difference whether
'x' has the same type or different types in the two definitions, you
have broken a rule and have undefined behavior.
Once you have undefined behavior, it's "game over, thanks for playing,
Carol will give you some lovely parting gifts". The C language
neither knows or cares what happens next, there is no right or wrong.
It could cause the CD drive tray to jump out and hit you in the nose.
Even if the computer does not have a CD drive.
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."
--
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: Allin Cottrell
- Re: Help wanted on some source codes
- References:
- Help wanted on some source codes
- From: Becker
- 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
|