Atexit () and exit () functions

Source: Internet
Author: User
Tags terminates

function exit ()


Function Prototypes:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/DC/wKiom1diskGxc9mDAAAHqscUHgo576.png "title=" exit1. PNG "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:200px;height:34px; "alt=" Wkiom1diskgxc9mdaaahqscuhgo576.png "/>

The exit () function is used to end the program at any time while the program is running, the parameter state of exit will be returned to the operating system , return 0 indicates that the program ends normally, and not 0 indicates that the program is not properly terminated. The Exit function is also implicitly called at the end of the main function. The Exit function runs with a function that is enlisted by the atexit () function, and then does some of its own cleanup work, flushing all output streams, closing all open streams, and closing temporary files created by standard I/O functions tmpfile ().

exit () end the current process/current program/, in the entire program, as long as the call exit, the end;

Exit (1) indicates that the process exited gracefully. returns 1;

Exit (0) indicates that the process is not properly exited. Returns 0.


Atexit () function

as specified in ISO C, a process can register up to 32 functions, which are automatically called by exit, and typically these 32 functions are called Terminating handlers , and call the Atexit function to register the functions.

It is often necessary to do something, such as releasing resources, when the program exits, but there are many ways to exit the program. Therefore, a method that is independent of the program exit mode is required to process the program exit.   the atexit () function is used to register a function to be called when the program terminates normally .

Here's a look at the prototype of the Atexit () function:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/DB/wKiom1dindby2sxeAAAgOgQMZ-s578.png "title=" Atexit1. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:86px; "alt=" Wkiom1dindby2sxeaaagogqmz-s578.png "/>

Atexit parameter is a function pointer , when this function is called without passing any arguments, the function cannot return a value, the Atexit function is called the termination handler registration program , after the registration is completed, When the function terminates, the Exit function invokes the previously registered functions, but the order in which the exit function calls these functions is the opposite of the order in which the functions are enlisted, which I think is essentially the result of the argument compaction, and the parameters are first in and out of the stack order. At the same time, if a function is registered multiple times, the function will be executed more than once.


Code snippet:

#include <stdio.h> #include <atdlib.h>void fun1 (void) {printf ("fun1\n");} void fun2 (void) {printf ("fun2\n");} void Fun3 (void) {printf ("fun3\n");}    int main () {atexit (FUN1);    Atexit (FUN2);        Atexit (FUN3); printf ("main\n");}

The output of the program run result is:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/DA/wKioL1dipGvA2xqnAAAkh2cp7v8009.png "title=" Atexit2. PNG "width=" "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:400PX;HEIGHT:119PX; "alt=" Wkiol1dipgva2xqnaaakh2cp7v8009.png "/>

These functions are not called until the end of main. Atexit just registers them so that they are called after the end of main and can be seen by name.



This article from the "777 Fast" blog, declined reprint!

Atexit () and exit () functions

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.