C ++ zhong_onexit () Usage

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.