views:

3305

answers:

5

Should I use exit() or just return statements in main()? Personally I favor the 'return' statements 'cause I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to refactor the main() function, having 'return' seems like a better choice than exit().

Does exit() do anything special that 'return' doesn't?

Thanks for your time!

+8  A: 

In C, return in main will return to the _start function in libc which will call main and after that invokes exit system call, so there's no special difference. I prefer return as it's more stylish.

Mehrdad Afshari
This isn't entirely correct when dealing with C++. Or rather, your comment is correct, but incomplete.
FreeMemory
@FreeMemory True, I edited my comment to specify "In C," explicitly.
Mehrdad Afshari
Nit-pick alert, but this is target-specific -- there is no requirement that there be anything, let alone with a particular name, that calls main. The compiler seems free to treat returns from main as special (according to my reading of the standard anyway).
brone
Yes, it was just an example, but it's nevertheless the same, whatever the name of _start be, and even if the main would be a special function, somehow the compiler should inject the exit after it. It doesn't make much difference here though.
Mehrdad Afshari
+3  A: 

I always use return because the standard prototype for main() says that it does return an int.

That said, some versions of the standards give main special treatment and assume that it returns 0 if there's no explicit return statement. Given the following code:

int foo() {}
int main(int argc, char *argv[]) {}

G++ only generates a warning for foo() and ignores the missing return from main:

% g++ -Wall -c foo.cc
foo.cc: In function ‘int foo()’:
foo.cc:1: warning: control reaches end of non-void function
Alnitak
I don't know about C, but the C++ standard specifies that if you don't return a value in main, it's assumed to return 0.
Jason Baker
Jason Baker
C99 and C++ return 0 if there is no return statement, C90 doesn't.
d0k
+3  A: 

Even though there is not much of a difference, i strongly prefer return to keep the natural flow of an application.

mafutrct
+57  A: 

Actually, there is a difference, but it's subtle. It has more implications for C++, but the differences are important.

When I call return in main(), destructors will be called for my locally scoped objects. If I call exit(), no destructor will be called for my locally scoped objects! Re-read that. exit() does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all your data flushed to disk?).

Note that static objects will be cleaned up even if you call exit(). Finally note, that if you use abort(), no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.

Proceed with caution when favoring exit over return.

http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a

FreeMemory
abort() exits with error condition (non-zero exit code) and may be even a core. If you need to exit w/o calling static destructors, use _exit .
Arkadiy
@Arkadiy **Absolutely.** abort signals some error condition. And, stylistically, I usually also use exit() to signal an error condition, and return 0 from main to indicate normal termination.
FreeMemory
From the exit() manpage: 2. Flush all open output streams. It sounds like any open output streams would be flushes regardless of whether the appropriate destructor is called.
Mike Koval
+9  A: 

Another difference: exit is a Standard Library function so you need to include headers and link with the standard library. To illustrate (in C++), this is a valid program:

int main() { return 0; }

but to use exit you'll need an include:

#include <cstdlib>
int main() { exit( EXIT_SUCCESS ); }

Plus this adds an additional assumption: that calling exit from main has the same side effects as returning zero. As others have pointed out, this depends on what kind of executable you're building (i.e., who's calling main). Are you coding an app that uses the C-runtime? A Maya plugin? A Windows service? A driver? Each case will require research to see if exit is equivalent to return. IMHO using exit when you really mean return just makes the code more confusing. OTOH, if you really do mean exit, then by all means use it.

jwfearn