Let's see if I can amalgamate the various links provided by others into an appropriate answer.
As far as the C language is concerned, there is no difference.
A call to exit()
does
- call the functions registered with
atexit()
;
- flushes and closes the output streams;
- removes files opened with
tmpfile()
;
- returns control to the environment.
A return <n>
from main is equivalent to exit( <n> )
.
Some flawed static code analyzers think different.
As far as the C language is concerned, memory allocated by main()
and not free()
d is leaked. Unix cleans up at process termination, but not all operating systems do (!).
Apparently, some static code analyzers consider memory still allocated at the point of exit()
to be not leaked (while they do for return
from main()
), which is why that commit was made (to get rid of the warning).
This is a workaround for a bug in the code analyzer.
As far as the C++ language is concerned, there is a lot of difference.
When you return
from main()
, you are leaving the scope of the function, which means local objects get destroyed.
Since C++ programmers enjoy the benefits of deterministic destruction (as opposed to e.g. Java which might or might not execute your destructor even on VM termination...), they tend to make their destructors do more than just freeing memory. Network connections, temporary files, terminal windows locked by ncurses
, all that kind of goodness, which C programmers would have to care for manually or use atexit()
for.
When you call std::exit()
from main()
, that function directly turns over control to the runtime. The main()
function never returns, the process gets terminated without calling the destructors of main()
-local objects.
Moreover, while std::exit()
is defined to flush and close the C output streams, there is no such provision for the C++ output streams.