C ++ zhong_onexit () Usage
Question: Is it possible to execute another code after the main function is executed?
Answer: Yes. You can use _ onexit to register a function. It will be executed after main.
Knowledge:
(1) use the format: _ onexit (int fun (), where the function fun () must be a non-parameter function with the int type returned value;
(2) _ onexit () is included in the header file cstdlib. cstdlib is a library function in C;
(3) Whether the function _ onexit () is put in any position in main, it is the final execution.
Program example analysis:
# Include
# Include
Using namespace std;
Int func1 (), func2 (), func3 ();
Int main (int argc, char * argv []) {
_ Onexit (func2 );
_ Onexit (func1); // The execution sequence of the combined three statements is arranged continuously here
_ Onexit (func3 );
Cout <"First Line" < Cout <"Second Line" < }
Int func1 ()
{
Cout <"fun1 () executed! "< Return 0;
}
Int func2 ()
{
Cout <"fun2 () executed! "< Return 0;
}
Int func3 ()
{
Cout <"fun3 () executed! "< Return 0;
}
According to the execution sequence of the combined _ onexit (func2); _ onexit (func1); _ onexit (func3);, we can see that _ onexit () is closer to the back of main, the execution sequence is higher, that is, the execution is delayed, which is similar to the 'stack' (Advanced and later.
Ps: due to the limited technical skills of the author, if there are errors and inconveniences, please kindly advise me!