Thread: main() return value standard - stupid question

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    22

    main() return value standard - stupid question

    I feel kinda dumb asking this question, but I'm in an argument with someone about whether or not to use "return 0;" at the end of main(). I'm not sure if I'm reading the standard wrong, but shouldn't a program return with a value of zero on success? Maybe I'm being inept at searching tonight, but I didn't find any answers than answered my question.

    Not including the "return 0;" at the end of main() would not guarantee this. The actual return value would be fairly unpredictable.

    Basically is there a standard that says "return 0;" should appear at the end of main()? Are there good reasons NOT to return a value of zero on a successful program execution?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My copy of C99 states:
    "If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified." (emphasis mine)

    So, if you do not explicitly return a value from main(), main() will return 0.

    On the other hand, I understand that C89 requires that some value be explicitly returned from main(), 0 meaning success.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    return 0;
    return EXIT_SUCCESS;
    return EXIT_FAILURE;
    These are the good things to return.
    Any other value is implementation specific.
    Not returning anything at all is undefined.

    In new C++ (only, I think), you can get away with omitting a return statement, and the compiler will add "return 0;" for you. But since this trick only works with main, it seems like a sop to all the void main programmers to make it easier for the poor dears to make their programs compliant by only changing 'void' to 'int'

    <edit>
    apparently, C99 has the same soppyness as well.
    </edit>


    > Are there good reasons NOT to return a value of zero on a successful program execution?
    The grep program is an example.
    It returns 0 if something is found, and 1 if nothing is found.
    As far as the grep program itself is concerned, it ran to completion successfully. It wasn't its fault that nothing could be found.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Well, 99.9&#37; of the time you want main() to return 0 at the closing brace, so I think it's reasonable to make it happen by default - and taking advantage of this reduces the chance that someone will forget that 0 is the normal return value, and write "return 1;" by mistake. And without the implicit "return 0;", there's no way to ensure that main() always has a well-defined return value - unless the compiler requires an explicit return at the closing brace, but in general there's no way to be sure if the closing brace will be reached, which is probably why compilers like gcc seem to allow functions with a return value but no explicit return statement to compile.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > On the other hand, I understand that C89 requires that some value be explicitly returned from main(), 0
    > meaning success.

    I believe that C89 doesn't actually require an explicit return statement in main(), but that without one, the return value is undefined - gcc seems to confirm this, since no explicit return leads only to a warning, not an error saying that ISO C90 requires such-and-such.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    22

    Post

    Ah, makes sense. So it's more just a good idea to include the 'return 0;' but not really required. Not that it's hard to type that one extra nine or so characters. I suppose it just bugs me because it gives unpredictable behavior.

    Thanks for helping clarify.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You never know when someone else will check the return value of your program, even if you don't, so it's a good idea to always put it in (in C, anyway - I leave it out in C++).

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    22
    Quote Originally Posted by robatino View Post
    You never know when someone else will check the return value of your program, even if you don't, so it's a good idea to always put it in (in C, anyway - I leave it out in C++).
    Those were my thoughts (well minus the C++ part since I don't know all of the basics of that language). I was explaining to someone why I thought they should always have a defined return value at the end of their program when someone else said only put it in if you have to so I started arguing with the second person about it. Unpredictable behavior just bugs me.
    Last edited by shuuhen; 09-23-2007 at 01:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union Question
    By estarapapax in forum C Programming
    Replies: 0
    Last Post: 10-21-2008, 10:08 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM