问题
Hi I wanted to know what the differences between returning and exiting from the main function are. What happens behind the scenes when each of them are invoked, and how is the control returned in each case. I would really appreaciate it if someone could dive deep into this subject.
回答1:
There is no difference.
Behind the scenes, what happens (at least on some popular operating systems) is this:
// Set up argc and argv
int retcode = main(argc, argv);
exit(retcode);
That behaviour is guaranteed by the C standard:
... a return from the initial call to the
mainfunction is equivalent to calling theexitfunction with the value returned by themainfunction as its argument ... (§5.1.2.2.3)
回答2:
There is basically no difference. However, exit() is useful because it allows you to exit the program from other functions different than main().
The only formal difference is that:
...the lifetimes of objects with automatic storage duration declared in main will have ended...
in the case of returning from main.
If you need more details, I suggest you read the latest C standard, in particular section 5.1.2.2.3:
...a return from the initial call to the
mainfunction is equivalent to calling theexitfunction with the value returned by themainfunction as its argument...
As well as section 7.22.4.4:
The
exitfunction causes normal program termination to occur...
Note that there are also other ways to exit a program, like abort, quick_exit and _Exit.
回答3:
Beyond other answers saying the two approaches are virtually the same:
From the assembly programmer's point of view, the primary difference when using exit() is that we don't have to (save or) restore the callee-saves registers, or the return address (if the ABI puts that in a register, that is).
If main returns to its caller then main ought to honor the ABI's register preservation requirements.
回答4:
C 2018 5.1.2.2.3 1 tells us what happens in a hosted environment:
If the return type of the
mainfunction is a type compatible withint, a return from the initial call to themainfunction is equivalent to calling theexitfunction with the value returned by themainfunction as its argument; reaching the}that terminates themainfunction returns a value of 0. If the return type is not compatible withint, the termination status returned to the host environment is unspecified.
So, in a hosted environment, what you likely think of as the “normal” C environment, return x; from the initial call to main is equivalent to exit(x);, if it was declared with a return type compatible with int. (C implementations may define other allowed declarations.)
In a freestanding environment, 5.1.2.1 2 tells us:
The effect of program termination in a freestanding environment is implementation-defined.
The behavior of exit is specified in 7.22.4.4:
3 First, all functions registered by the
atexitfunction are called, in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered…
4 Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the
tmpfilefunction are removed.
5 Finally, control is returned to the host environment. If the value of
status[the parameter ofexit] is zero orEXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value ofstatusisEXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
来源:https://stackoverflow.com/questions/63309629/return-vs-exit-from-main-function-in-c