Thread: Exit_success

  1. #1
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86

    Exit_success

    if main should return 0, why there exist EXIT_SUCCESS?

    do I have to do this :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
        /* blablabla... */
    
        return(EXIT_SUCCESS);
    }
    to make my program portable?
    Do not stare at my avatar.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    main() is not required to return zero. It is allowed to return any int value.

    By convention, if main() returns zero or EXIT_SUCCESS (if the values are different) then the program is deemed to have been terminated successfully. So, to indicate success, you are allowed to return 0 or EXIT_SUCCESS. If the macro EXIT_SUCCESS expands to zero then there is no difference between the two options.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    if main should return 0, why there exist EXIT_SUCCESS?
    My guess is for symmetry with EXIT_FAILURE. I suspect that before C was standardized, 0 was universally recognized as a successful exit (at least as far as C implementations go), but there were different ideas for what non-zero codes meant. Thus EXIT_FAILURE was invented to allow a portable program to signal failure; and since there was an EXIT_FAILURE, why not an EXIT_SUCCESS as well? 0 would still be success, but now you can use a macro if you'd like. Why EXIT_SUCCESS isn't simply defined to be 0 I'm not sure, since 0 is required to mean success as well.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cas View Post
    My guess is for symmetry with EXIT_FAILURE. I suspect that before C was standardized, 0 was universally recognized as a successful exit (at least as far as C implementations go), but there were different ideas for what non-zero codes meant. Thus EXIT_FAILURE was invented to allow a portable program to signal failure; and since there was an EXIT_FAILURE, why not an EXIT_SUCCESS as well? 0 would still be success, but now you can use a macro if you'd like. Why EXIT_SUCCESS isn't simply defined to be 0 I'm not sure, since 0 is required to mean success as well.
    Actually... it's done so the program can inform it's host process of errors that occured.

    For Example: Batch files (in Windows command shells) can actually use the returned values in conditional branching... IF ERRORLEVEL== and GOTO are commonly used as a means to control the behavior of complex batch processes.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    One other reasons is the portability and the code readability. Using macros would ofcourse make much easier in reading code. If for example
    Code:
    #define SIG_AUTH_REQ   010110
    #define SIG_AUTH_RSP   001010
    
    if SIG_RECV == SIG_AUTH_REG
        calculate AUTH_REP
        send SIG_AUTH_RSP
    Of course you would use the binary there, instead the hex would be used. But you understand the point I’m trying to make.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    does EXIT_SUCCESS always expand to 0 on all system/OS ?
    Do not stare at my avatar.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cph View Post
    does EXIT_SUCCESS always expand to 0 on all system/OS ?
    Probably... but no quarantees.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The C standard allows EXIT_SUCCESS to expand to a value other than zero. In practice, I've yet to encounter a system where EXIT_SUCCESS does not expand to zero. However, there are quite a few systems I have not worked with.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Same thread from years ago...
    exit(-3)
    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.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by CommonTater View Post
    Actually... it's done so the program can inform it's host process of errors that occured.

    For Example: Batch files (in Windows command shells) can actually use the returned values in conditional branching... IF ERRORLEVEL== and GOTO are commonly used as a means to control the behavior of complex batch processes.
    You misunderstood.

    If you reread my post, you'll see that I'm talking solely about why EXIT_SUCCESS exists when it needn't (since this was the OP's question).

    EXIT_FAILURE still exists to allow a portable program to signal failure, as I said. You cannot signal exit failure in a portable manner without using EXIT_FAILURE, but you can signal exit success in a portable manner without using EXIT_SUCCESS.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In practice, the OS does not care what value a user program exits with. The only software which looks at user program exit values is other user programs. It's up to these programs to define what the exit codes mean. EXIT_SUCCESS and EXIT_FAILURE are suggested values to use to indicate success or failure.

    The OS doesn't care a whit about whether your program "succeeded" or "failed," it doesn't even define what these codes mean, it just takes the exit code and stores it somewhere so another program can check what value it is.

    There's nothing stopping me from writing a program which launches another program, gets its exit code, and treats the value 0 as a failure. It's just not conventional.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    They're the same thing. Most people recognize 0 as being the successful return value. I'm sure return (EXIT_SUCCESS) doesn't return anything different.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  13. #13
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Thanks to everyone for the explanation

    Quote Originally Posted by Salem View Post
    Same thread from years ago...
    exit(-3)
    sorry, I should have done some searching first
    Do not stare at my avatar.

Popular pages Recent additions subscribe to a feed