Code to be executed after the main function is executed

Source: Internet
Author: User

In fact, I don't want to talk nonsense, but I do have a lot of nonsense. Just now, the voltage suddenly changed, and my computer was automatically shut down. The consequences, you know.

I used to think that after the main function is executed, the return value will be returned to the operating system. Even if the program's own operations are over, all future operations will be handed over to the operating system. I read a C/C ++ interview question last night and found that other functions can be executed after the main function is executed. Okay, maybe you have to despise me. You can see that the experts you know can pass through. If you don't know, let's get to know it. The code is presented as follows:

//
// If you need to execute a piece of code after the main function is executed,
// You can use the atexit function to register one or more functions that are registered in the stack,
// Called after the main function is completed. The call sequence is the opposite to the registration sequence.
//

# Include <stdlib. h>
# Include <stdio. h>

//
// Declare the function to be executed after the main function is completed.
// This kind of function pre-declaration is very special! I have never thought of it before.
//
Void fn1 (void), FN2 (void), fn3 (void), FN4 (void );


Int main (void)
{
//
// Register the function to be executed after the main function is completed.
// Pay attention to their registration order and execution order to see how interesting their output results are!
//
Atexit (fn1 );
Atexit (FN2 );
Atexit (fn3 );
Atexit (FN4 );

// This output statement has reference, but it is not the last output.
Puts ("this is executed first .");

//
// Exit_success indicates 0, which is defined in stdlib. h.
// By the way, you may know, but I am worried that you do not know.
//
Return exit_success;
}


Void fn1 (void)
{
Printf ("next. \ n ");
}


Void FN2 (void)
{
Printf ("executed ");
}


Void fn3 (void)
{
Printf ("is ");
}


Void FN4 (void)
{
Printf ("this ");
}

Maybe you are too lazy to try IDE, so let's take a look at the running effect!

This is executed first.
This is executed next.

For more information about the atexit function, the prototype is as follows:

int atexit(void (*func)(void));

The atexit function is added to Standard C. It "registers" a function so that this function will be called when the exit function is called or when the mian function returns. When a program ends abnormally (such as calling abort or raise), functions registered through it are not called. The compiler must allow programmers to register at least 32 functions. If the registration is successful, atexit returns 0; otherwise, a non-zero value is returned. There is no way to cancel the registration of a function. Prior to any standard cleanup operation executed by exit, the registered functions are called sequentially in the reverse order of registration. Each called function does not accept any parameters and the return type is void. A registered function should not attempt to reference any objects whose storage class is auto or register (for example, through pointers) unless it is defined by itself. Multiple registration of the same function will cause this function to be called multiple times. Some traditional C compilers use the onexit name to implement functions like yes.

I copied the above passage from the C language reference manual. Haha!

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.