Re: Help wanted on some source codes



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
.



Relevant Pages

  • Re: Sequence Point before actual function call
    ... void func ... int main ... The C Standard says that there is a sequence point before the actual ... function and needs a declaration */ ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... including one of them might trivially slow down compilation, ... I wrote the above (starting with "The standard headers"). ... return void, but there's no advantage in using that. ... You're cheating yourself by ignoring warning messages. ...
    (comp.lang.c)
  • Re: help w/ c/c++ problem
    ... | Then I'm afraid that both your book and your teacher are wrong. ... I'll admit to it when the persons who spent several hundred posts ... standard explicitly allows ... 'void main', then a program that uses 'void main' is valid ...
    (comp.lang.c)
  • Re: Best book on learning C++?
    ... >> because they were told to by their (incompetent) teacher. ... >the mix of things I have always used void main. ... >Me as the customer wouldn't care how the software was programmed as long as ... Actually lots intendes to follow the standard. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Memory allocation for an initialized character pointer;
    ... But, speaking strictly in regard to the standard, we can say it is ... Where does the Standard explicitly say that void mainis ... a strictly conforming program can't rely on the order of bit- ... In C89, it's incorrect. ...
    (comp.lang.c)