2) before and after the main function is executed

Source: Internet
Author: User

2) before and after the main function is executed

Before the main function is executed, it mainly initializes system-related resources: 1. set the stack pointer 2. initialize static and global variables, that is, the content of the data segment 3. assign the uninitialized global variables to the initial values: Numeric short, int, long, etc. 0, bool is FALSE, pointer is NULL, and so on. bss segment content 4. global object initialization, calling the constructor before main 5. pass the parameters of the main function, argc, argv, and so on to the main function, and then truly run the main function.

After the main function is executed: 1. The Global Object destructor will be executed after the main function; 2. You can register a function with _ onexit, which will be executed after the main function;
If you need to add a piece of code that is executed after the main exit, you can use the onexit () function to register a function.
# Include <stdlib. h>
# Include <stdio. h>

Int atexit (void (* function ") (void ));
Void fn1 (void), fn2 (void), fn3 (void );

Int main (void ){
Onexit (fn1 );
Onexit (fn2 );
Printf ("This is executed first .");
}

Void fn1 (){
Printf ("This is ");
}
Void fn2 (){
Printf ("executed next .");
}
Result: This is executed first. This is executed next.

Related Article

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.