Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Return codes

  1. #1
    Join Date
    Mar 2007
    Location
    SomeWhere, Michigan
    Posts
    246

    Return codes

    Just wondering if there are different return codes other than 0 (success) 1 (failed)

    int main()
    {
    // do something

    return 0;
    // return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Mar 2003
    Location
    Colorful Colorado
    Posts
    1,096
    yes. typically any non-zero number is an error and the value of the number indicates what the error is. for example 1 = "file not found", 2 = "divide by zero", etc.
    Chuck
    Digital Spectra
    (rendering with pixels)

  3. #3
    Join Date
    Jun 2005
    Location
    NY
    Posts
    1,374
    Well it depends on what type of return codes the function has, a bool return value can only be true or false, an int return code can be any valid integer, a char return value can be anything from -128 - 127.
    You can also define your own return codes using #define directives.

    That is the purpose of return codes so that you can check when something fails or not.

    example:
    Code:
    int MyFunc()
    {
        if (...) // imagine some error check
           return -1;
    
        if (...) // imagine some other error check
           return -2;
    
        return 0; // Function succeeded
    }
    
    int main()
    {
        if (MyFunc() != 0)
            return 1; // The function failed, exit out
    
        return 0; // the function succeeded
    }
    Last edited by Notsosuperhero; 09-26-2007 at 12:58 AM.
    CodeGuru: DLL Tutorial For Beginners by me. Rated 4 1/2 out of 5.

  4. #4
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    753
    Or the more modern method is to pass exceptions, which can be tested and dealt with.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  5. #5
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Exceptions are expensive and aren't always the right solution. Exceptions should be used for exceptional cases, return codes have a lot of different applications.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  6. #6
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Posts
    501
    I came up with a question like this not too long ago. One of my instructors insisted we returned EXIT_SUCCESS. He claimed that not all operating systems use the return value of 0 to indicate a successful exit.

    I looked up how VS declares EXIT_SUCCESS and it pretty much just defines it as 0. Since the definition was so straight forward I just let the question slip to the back of my mind.

    Does anyone know of any architecture or OS that does not use the value 0 to indicate a successful exit?

  7. #7
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    No... I don't know a single one because it is usually used to chain together multiple command line apps based on the success or failure of the previous one.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  8. #8
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    basically if you're doing it from a batch file, if its not 0 its an error, just go with that. Error codes are specific to the app.. and in fact, some apps dont give them.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  9. #9
    Join Date
    Mar 2007
    Location
    SomeWhere, Michigan
    Posts
    246
    so its the OS that knows what the return codes are and NOT the IDE (compiler)

    I also wondered why some people used EXIT_SUCCESS instead of RETURN 0... now I know that they are the same pretty much.

  10. #10
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Posts
    501
    Thanks, it seemed rather silly to me to have to include cstdlib on everything I made, just to have EXIT_SUCCESS. But hey, anything for a good grade, right?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •