Return vs Exit from main function in C [duplicate]

守給你的承諾、 提交于 2020-08-20 03:16:14

问题


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 main function is equivalent to calling the exit function with the value returned by the main function 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 main function is equivalent to calling the exit function with the value returned by the main function as its argument...

As well as section 7.22.4.4:

The exit function 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 main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, 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 atexit function 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 tmpfile function are removed.

5 Finally, control is returned to the host environment. If the value of status [the parameter of exit] is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!