[LCP]Why do I do this? or exit() vs. return

robert at ncdesigns.net robert at ncdesigns.net
Thu Jul 10 08:20:29 UTC 2003


Here's a question that is more about style I guess.
I know that's quite subjective.

Take a look at these versions of 

#include <stdio.h>

int main( int argc, char **argv )
{
    int x = 4;
    int y = 3;

    if (x > y)
    {
        return 1;
    }

    return 0;
}


Which is better? I prefer the above style, but...others I know do this:

    if (x > y)
    {
        exit(1);
    }

exit(0);
}

and here is what I'm told is the "proper" way of doing things.


    if (x > y)
    {
        return EXIT_FAILURE;
    }

return EXIT_SUCCESS;
}

It would seem the only reason to use exit() semantics is if you wanted to 
attach cleanup procedures? using the atexit() and on_exit() functions.

But...my fellow programmers use these all the time. I try and discourage 
the use since we're have no needs for the chaining of exit functions, I'm 
at a loss why I do things this way. I would like a viable argument for 
either.

I know that last case is what I should be doing since it's more portable, 
so maybe that is the only way to go.

Actually it's quite annoying to get compile warnings when a programmer 
forgets to add the header for exit(), but that is another issue. :)

Rpbert




More information about the linuxCprogramming mailing list