Re: Big Deal with returning int

Discussion in 'C++' started by Rolf Magnus, Jun 29, 2003.

  1. Rolf Magnus

    Rolf Magnus Guest

    Min wrote:

    > 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" ?


    void main() is just plain wrong. Some compilers support it as an
    (actually unneccesary) extension to the language, so on those specific
    compilers, it might work, but that's not guaranteed. The stanard C++
    language (as C) _never_ permited any other return type than int. This
    rule is older than some programmers, and still there are people who get
    it wrong. Btw, if everyone keeps telling you that void main() is wrong,
    what's the big deal about changing it to int and just using that?

    > 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.
    >
    > Or How is it possibly cause the problem ?


    A compiler may not accept it, and it is supposed to issue at least a
    warning if you try to return void from main. Also, on many systems, the
    exit code (i.e. the value returned from main) is important for the
    parent process. If you return void, the parent may get garbage instead
    of a reasonable return code. A system also is (from the C++ standard's
    POV) allowed to crash completely if no sensible value is returned. I
    don't actually know a system that does that, but this means nothing.

    > 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.


    I see it the same way. If some C++ book or tutorial has examples that
    contain "void main", I'd throw it away. As you say, if they don't get
    such a fundamental and simple thing right (or just don't know that it's
    wrong), then they probably don't know the C++ language very well, and
    other more severe flaws are likely to be lurking there.
     
    Rolf Magnus, Jun 29, 2003
    #1
    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:
    662
  2. John Harrison

    Re: Big Deal with returning int

    John Harrison, Jun 29, 2003, in forum: C++
    Replies:
    5
    Views:
    831
    Karl Heinz Buchegger
    Jun 30, 2003
  3. Kelsey Bjarnason

    Re: Big Deal with returning int

    Kelsey Bjarnason, Jun 29, 2003, in forum: C++
    Replies:
    38
    Views:
    1,639
    Andre Kostur
    Jul 1, 2003
  4. Samuele Armondi

    Re: Big Deal with returning int

    Samuele Armondi, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    696
    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,672
    Rolf Magnus
    Jul 1, 2003
Loading...

Share This Page