2 very basic qns

Discussion in 'C++' started by mescaline, Dec 29, 2003.

  1. mescaline

    mescaline Guest

    1. Why does C++ require the int in:
    int main(){...}
    (is there a special reason for int, why not void,etc...?)

    2. What purpose does the return 0; serve at the end of the main
    program:
    (when the program is run successfully and the " return 0; " statement
    is encountered, what exactly happens -- where is the 0 returned?)

    thanks
     
    mescaline, Dec 29, 2003
    #1
    1. Advertising

  2. mescaline

    Ron Natalie Guest

    "mescaline" <> wrote in message news:...
    > 1. Why does C++ require the int in:
    > int main(){...}
    > (is there a special reason for int, why not void,etc...?)


    Are you talking about main specifically or functions in general. The "implicit"
    int return has been removed from the language because it wasn't exactly consistant.

    As far as why MAIN itself must return int. Tradition is the major reason.

    >
    > 2. What purpose does the return 0; serve at the end of the main
    > program:
    > (when the program is run successfully and the " return 0; " statement
    > is encountered, what exactly happens -- where is the 0 returned?)
    >

    It's an implementation specific success code. On some systems it gets
    passed back to the invoked program (like UNIX). On some systems
    the environment calls a system call indicating normal termination where as
    the error returns from exit/main call the error system call. On others a
    zero means silent exit while a non-zero return cause an error popup.
    Others the value is just discarded.
     
    Ron Natalie, Dec 29, 2003
    #2
    1. Advertising

  3. "Ron Natalie" <> wrote in message
    news:3fef4a9c$0$31832$...
    >
    > "mescaline" <> wrote in message

    news:...
    > > 1. Why does C++ require the int in:
    > > int main(){...}
    > > (is there a special reason for int, why not void,etc...?)

    >
    > Are you talking about main specifically or functions in general. The

    "implicit"
    > int return has been removed from the language because it wasn't exactly

    consistant.
    >
    > As far as why MAIN itself must return int. Tradition is the major

    reason.

    Well, not really. The standards group, that works on defining the language,
    has published a standard that main must return an int.
    Unless you want to call standards a tradition...
    --
    Gary
     
    Gary Labowitz, Dec 29, 2003
    #3
  4. mescaline

    Ron Natalie Guest

    "Gary Labowitz" <> wrote in message news:...
    > Well, not really. The standards group, that works on defining the language,
    > has published a standard that main must return an int.
    > Unless you want to call standards a tradition...
    > --

    I know that. The poster asked for WHY. The language is defined by the
    standard, but the standards people had reasons for the requirements they
    set down. The reason main is REQUIRED BY THE STANDARD
    to return in is TRADITION. Main has always returned int, ever since
    the early days of C. Partially this was because C didn't even have a void
    type in the beginning, then later because UNIX relied heavily on using the
    numeric return from programs.
     
    Ron Natalie, Dec 29, 2003
    #4
  5. mescaline

    mescaline Guest

    "Gary Labowitz" <> wrote in message news:<>...
    > "Ron Natalie" <> wrote in message
    > news:3fef4a9c$0$31832$...
    > >
    > > "mescaline" <> wrote in message

    > news:...
    > > > 1. Why does C++ require the int in:
    > > > int main(){...}
    > > > (is there a special reason for int, why not void,etc...?)

    > >
    > > Are you talking about main specifically or functions in general. The

    > "implicit"
    > > int return has been removed from the language because it wasn't exactly

    > consistant.
    > >
    > > As far as why MAIN itself must return int. Tradition is the major

    > reason.
    >
    > Well, not really. The standards group, that works on defining the language,
    > has published a standard that main must return an int.
    > Unless you want to call standards a tradition...



    Conversely, does it always return a non-zero value, when there's *any*
    error with the compiling?

    thanks
     
    mescaline, Dec 29, 2003
    #5
  6. mescaline

    Ron Natalie Guest

    "mescaline" <> wrote in message news:...
    >> Conversely, does it always return a non-zero value, when there's *any*

    > error with the compiling?
    >


    If you had problems compiling, you're not likely going to get as far as
    having a main function to run.
     
    Ron Natalie, Dec 29, 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. Jin

    Misc Qns...

    Jin, Dec 16, 2003, in forum: C Programming
    Replies:
    20
    Views:
    1,057
    Dave Thompson
    Dec 25, 2003
  2. Qns on blank lines

    , Mar 3, 2006, in forum: Python
    Replies:
    0
    Views:
    277
  3. a subprocess qns

    , Apr 21, 2006, in forum: Python
    Replies:
    2
    Views:
    304
    Ben C
    Apr 21, 2006
  4. Replies:
    3
    Views:
    270
    Tim Chase
    Jun 1, 2006
  5. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    519
    Dave Thompson
    Feb 14, 2005
Loading...

Share This Page