John Harrison wrote:
>
> "Klaus Eichner" <> wrote in message
> news:3efea6fa$0$12457$...
> > "Min" <> wrote in message
> > news:VgvLa.341602$ a...
> > > I have seen so many pointing out "main" should explicitly return "int".
> > > Beside, the language spec or committee, or some guru said so, what is a
> > BIG
> > > deal with it ? What difference does it make if "void main" rather than
> > "int
> > > main" ? It is not that some obscure code that people can't understand
> > > easily. Anyway, would someone explain why ;? not just it is better to
> > > return explicitly.
> >
> > http://www.research.att.com/~bs/bs_faq2.html#void-main
>
> Well that provides a good explaination of what the situation is and from the
> horse's mouth, so to speak. But it still doesn't explain why.
Because in C and C++ it is defined to be that way. And it is that way because
one needs to have a way to inform the 'caller' of that function of certain
conditions. This is done with the help of the return type.
It is similar to:
int foo()
{
return 0;
}
int foo2()
{
int i;
i = foo();
}
foo returns an int and thus informs foo2 of certain things. Now if you decide
that foo no longer has some meaningfull return value, you change it to:
void foo()
{
}
int foo2()
{
int i;
i = foo();
}
But now you have a problem: you can't no longer use the return value of foo
for the assignment in foo2()! foo no longer returns anything! You have to change
foo2() too! So changing parameter passing mechanisms and return values allways
affects 2 sides: the caller and the callee.
Well. The exact analog situation is with main and the operating system. Your
main function equals foo(). And the operating system equals foo2(). If you
change the return type of foo() (your main()) you also have to change
the callers code. But hey! The callers code is out of your control! You
cannot change the way main is called from the OP. And the callers code
expects your main function to return an int! This is how it has been
defined years back!
--
Karl Heinz Buchegger