Exit () and atexit ()

Source: Internet
Author: User

I. Exit () function
Function declaration: void exit (INT State );

The exit () function is used to end the program at any time during the program running process. The exit parameter state is returned to the operating system. The return value 0 indicates that the program ends normally, and the non-0 indicates that the program ends abnormally.The exit function is also called implicitly at the end of the main function. When the exit function is run, it first executes the function registered by the atexit () function, and then cleans itself., Refresh all output streams, close all open streams, and close temporary files created using the standard I/O function tmpfile.

Bytes -----------------------------------------------------------------------------------------------------

Ii. atexit () function
Function declaration: int atexit (void (* func) (void ));
 
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 main () function execution ends, exit () is used to end the program somewhere in the program, and the user terminates the program through Ctrl + C or Ctrl + break, therefore, there must be a method unrelated to the program exit method to process the program exit. The method is to use the atexit () functionThe function to be called when the registration program ends normally.

The parameter of the atexit () function is a function pointer pointing to a function without parameters or returned values. The prototype of the atexit () function is: int atexit (void (*) (void ));

You can use atexit () to register up to 32 handler functions in a program. These handler functions are called in the opposite order of registration., That is, the last call of the first registration and the first call of the last registration.

The following is a sample code:
# Include <stdlib. h> // use the header file stdlib. h required by the atexit () function
# Include <iostream. h>

Void terminatetest ()
{
Cout <"The program is being completed..." <Endl;
}

Int main (void)
{
// Register the exit Handler
Atexit (terminatetest );

Cout <"the end of main ()" <Endl;

Return 0;
}

The program runs as follows:
The end of main ()
The program is being completed...

 

 

These functions are called only after the end of main.AtexitJust register them so that they will be called after the end of Main. You can see them by name.
AtexitThis is extremely useful. You can destroy global variables (classes) in the order you set. For example, there is a log class, you may also call the log class to write logs in other global classes. Therefore, the log class must be parsed. If no log parsing sequence is specified, the program may first parse the log class when exiting, and other global classes cannot write logs correctly at this time.

Writing data back to a file and deleting a temporary file is really useful.

Exit () and atexit ()

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.