SoFunction
Updated on 2025-04-06

A brief analysis of the differences between ending program functions exit, _exit, and atexit

Many times we need to do some operations such as releasing resources when the program exits, but there are many ways to exit the program, such as the main() function ending, the program is ended with exit() somewhere in the program, the user terminates the program through Ctrl+C or Ctrl+break operations, etc. Therefore, there is a method that is not related to the program exit method to perform necessary processing when the program exits. The method is to use the atexit() function to register the function to be called when the program terminates normally.

The parameter of the atexit() function is a function pointer, and the function pointer points to a function that has no parameters and no return value. The function prototype of atexit() is: int atexit (void (*)(void));

In a program, you can use atexit() to register up to 32 processing functions. The order of call of these processing functions is the opposite of the order they are registered, that is, the last call that is first registered and the first call that is last registered.

Copy the codeThe code is as follows:

#include <>
#include <>

void fun()
{
    printf("fun/n");
}

int main()
{
    atexit(fun);
    printf("hello/n");
    return 0;
}

// int atexit(void (*func)()) // See the definition in <>
// {
//     func();
//     return 0;
// }


The above code will output

hello

fun

After removing the red comment code, the library function is redefined due to the Interpositioning behavior, making atexit only manifested as an ordinary function.

Therefore the output

Copy the codeThe code is as follows:

fun

hello

#include <>

#define EXIT_FAILURE ...
#define EXIT_SUCCESS ...

void exit (int status);
void _Exit(int status);          //C99
void abort(void);

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


The exit, _Exit and abort functions terminate the program and control the caller that does not return to these functions.

The function exit terminates the program normally and performs the following cleaning operations:

1. (Into the standard C language) All functions that want to register atexit function are called in the opposite order from the time of registration, and will be called several times if the number of times is registered.

2. Refresh the open output stream and close all open data streams.

3. Delete the file generated by the tepfile function.

4. Control returns to the host environment and provides status values.

According to the custom in many systems, a status value of 0 indicates successful termination of the program, and a non-0 value indicates abnormal termination. The value of the numeric value 0 and macro EXIT_SCCESS in standard C language indicates successful termination, the value of the macro EXIT_FAILURE indicates unsuccessful termination, and the meaning of other values ​​is defined by the implementation. Returning an integer value from function main is equivalent to calling the exit function with this value.

The difference between the function _Exit and the exit function is that it neither calls the exit processor registered by atexit nor calls the signal processor registered by singal. Whether to perform other cleaning operations is defined by the implementation, such as closing all open data streams. _Exit is added by C99, and some implementations traditionally provide similar functions with functions named _exit.

abort function causes the program to terminate abnormally and does not call the function registered with atexit. Whether abort causes a cleanup operation is defined by the implementation, and the status value returned to the host system is also defined by the implementation, but should be expressed as "unsuccessful". In standard C and many traditional implementations, call abort is converted into a special signal that can be captured (SIGABRT in standard C). If the signal is ignored or the processor returns, the standard C implementation still terminates the program, while other implementations may cause the abort function to return to the caller. Abort will also be called if the assertion fails.

atexitFunctions are standardCAdded in the language,It registers a function,Make the callexittime or functionmainThis function will be called when returning。 When the program terminates abnormally(Ifabortorraisetermination),Registered functions are not called。 The implementation should allow at least 32 functions to be registered. If the registration is successful, the atexit function returns 0, otherwise it returns a non-0 value and the function cannot be logged out. All directionsatexitFunctions registered with functions are called in the opposite order as to register,ThenatexitFunctions complete all standard cleanup operations。 Each function is not called on behalf of the parameter and should have the return type void. Registered functions cannot refer to any storage class that is not defined by itself.autoor registerObject of(For example, referenced by pointer)。 The function will be called several times at this time when it is registered several times.

Example of pointer function usage:

Copy the codeThe code is as follows:

#include<>
#include<>

typedef void (*pFunc)(float a);

// int atexit(void (*func)())
// {
//     func();
//     return 0;
// }

int atexitf( void (*func)(float),float a)
{
    func(a);
    return 0;
}

void test(float a)
{
     printf("test %f/n",a);
}

// void fun()
// {
//     printf("fun/n");
// }

int main()
{
pFunc pFunc1 = (pFunc)test;  // Function pointer assignment
    pFunc1(4.5);
/*    atexit(fun);*/
    atexitf(pFunc1,3.66);
    atexitf(test,3.66);
    //atexitf(test(4.3));
    printf("hello/n");
    getchar();
    return 0;
}