Use of quick_exit function in C/C++ with Examples

quick_exit in c

The quick_exit function terminates the process normally without completely cleaning the resources. It defined in the ‘stdlib.h’ header file, so you have to include the header file before using it.

The quick_exit function does not invoke the functions registered with atexit. But call the functions registered by the atexit function, in the reverse order of their registration.

Syntax quick_exit in C:

//Syntax of quick_exit

_Noreturn void quick_exit(int status); // C11

Parameters:

status: Indicates whether the program terminated normally. It can be one of the following:

Value Description
EXIT_SUCCESS Successful termination
0 Successful termination
EXIT_FAILURE Unsuccessful termination

Return:

The quick_exit function cannot return to its caller.

 

Let’s see an example code to understand the quick_exit function in C. Example code does not execute functions registered with at_quick_exit.

Note: Only C11 compiler, compile this code.

 

#include <stdio.h>
#include <stdlib.h>

void TestFunQexit (void)
{
    puts ("Quick exit function.");
}

int main ()
{
    //registered function with at_quick_exit
    at_quick_exit(TestFunQexit);

    puts ("Main function: Beginning");

    //called quick_exit()
    quick_exit (EXIT_SUCCESS);

    // never executed
    puts ("Main function: End");

    return 0;
}

Output:
Main function: Beginning
Quick exit function.

 

Important points related to the quick_exit function in C:

1. You must include stdlib.h header file before using the quick_exit function in C.

2. The quick_exit function does not invoke the functions registered atexit.

3. call the functions registered by the atexit function, in the reverse order of their registration.

4. If a program calls the quick_exit function more than once, or calls the exit function in addition to the quick_exit function, the behavior is undefined.

5. The status returned to the host environment is determined in the same way as for the exit function.

  • If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
  • If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned.
  • In other cases, the implementation-defined status value is returned.

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *