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

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

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

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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    736
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    529
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    2,073
    Merrill & Michele
    Oct 19, 2004
  4. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    738
    jacob navia
    Oct 17, 2004
  5. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    841
    Dave Thompson
    Feb 14, 2005
  6. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,811
  7. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,570
    August Karlstrom
    Sep 26, 2008
  8. Sunjay Varma
    Replies:
    4
    Views:
    571
    John Roth
    Mar 10, 2011
Loading...