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

bruceb3 at zip.com.au bruceb3 at zip.com.au
Thu Jul 10 12:07:02 UTC 2003


On Wed, Jul 09, 2003 at 08:13:03PM -0400, robert at ncdesigns.net wrote:
> Here's a question that is more about style I guess.
> I know that's quite subjective.
> 
>     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);

Finding the _right_ answer to your question may 
prove difficult. Seems like this is more of
a matter of opinion.

I prefer ...

int main( int argc, char *argv[] )
{
    int return_value;
    
    int x = 3, y = 2;

    if( x > y ) {
	return_value = 1;
    } else {
        return_value = 0;
    }

    exit( return_value );

}

I would stick to using exit because at some
latter point the programmers may start using
atexit and on_exit, and if this happens you 
won't have to modify the code.

Beware of the differences between exit and 
_exit aswell.

Bruce.




More information about the linuxCprogramming mailing list