Re: Big Deal with returning int

Discussion in 'C++' started by John Harrison, Jun 29, 2003.

  1. "Klaus Eichner" <> wrote in message
    news:3efea6fa$0$12457$...
    > "Min" <> wrote in message
    > news:VgvLa.341602$...
    > > 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. I'm still
    contending that there is no good explaination.

    john
    John Harrison, Jun 29, 2003
    #1
    1. Advertising

  2. "Min" <> wrote in message
    news:VgvLa.341602$...
    > 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

    > Or How is it possibly cause the problem ?
    >
    > PS: I am saying this because most of C++ books I found, uses "void main".
    > If they can't get this fundamental thing right, I guess, I have been

    reading
    > all the garbage from them.
    Klaus Eichner, Jun 29, 2003
    #2
    1. Advertising

  3. > > >
    > > > 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. I'm still
    > > contending that there is no good explaination.

    >
    > I don't think there is a conclusive explanation in the standard,


    I wouldn't expect it to be there.

    > neither do
    > I believe that such an explanation is necessary.


    Not necessary perhaps, but it would be nice to know.

    > There is, however, a lot of
    > discussion out there, especially in comp.std.c++. See, for example, the
    > discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
    > comp.std.c++, date: 2003-04-11 02:52:13 PST


    Thanks, I'll take a look.

    john
    John Harrison, Jun 29, 2003
    #3
  4. >
    > > There is, however, a lot of
    > > discussion out there, especially in comp.std.c++. See, for example, the
    > > discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
    > > comp.std.c++, date: 2003-04-11 02:52:13 PST

    >
    > Thanks, I'll take a look.
    >
    > john


    Seems to me the reason is probably to do with the historical development of
    C and C++ (in particular the later introduction of void to C and the banning
    of implicit int return in C++). So no good objective reason just historical
    accident, which is fair enough.

    john
    John Harrison, Jun 29, 2003
    #4
  5. "John Harrison" <> wrote in message
    news:bdm98j$u6agh$...
    >
    > "Klaus Eichner" <> wrote in message
    > news:3efea6fa$0$12457$...
    > > "Min" <> wrote in message
    > > news:VgvLa.341602$...
    > > > 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. I'm still
    > contending that there is no good explaination.


    I don't think there is a conclusive explanation in the standard, neither do
    I believe that such an explanation is necessary. There is, however, a lot of
    discussion out there, especially in comp.std.c++. See, for example, the
    discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
    comp.std.c++, date: 2003-04-11 02:52:13 PST
    http://tinyurl.com/fjww

    > john
    >
    >
    Klaus Eichner, Jun 29, 2003
    #5
  6. John Harrison wrote:
    >
    > "Klaus Eichner" <> wrote in message
    > news:3efea6fa$0$12457$...
    > > "Min" <> wrote in message
    > > news:VgvLa.341602$...
    > > > 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
    Karl Heinz Buchegger, Jun 30, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    648
  2. Kelsey Bjarnason

    Re: Big Deal with returning int

    Kelsey Bjarnason, Jun 29, 2003, in forum: C++
    Replies:
    38
    Views:
    1,628
    Andre Kostur
    Jul 1, 2003
  3. Rolf Magnus

    Re: Big Deal with returning int

    Rolf Magnus, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    630
    Rolf Magnus
    Jun 29, 2003
  4. Samuele Armondi

    Re: Big Deal with returning int

    Samuele Armondi, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    690
    Samuele Armondi
    Jun 29, 2003
  5. Jonathan Clements

    Re: Big Deal with returning int

    Jonathan Clements, Jun 29, 2003, in forum: C++
    Replies:
    7
    Views:
    1,660
    Rolf Magnus
    Jul 1, 2003
Loading...

Share This Page