Use of exit function in C Language

Source: Internet
Author: User
Tags exit in

Exit () ends the current process/current program/. In the whole program, it ends when exit is called.

Return () is the return of the current function. Of course, if it is in the main function, the current process is naturally ended. If it is not, it is returned to the previous layer for calling. If you want to check whether the process Exits normally in multiple processes, you need to use the return value of the previous process ..

Exit (1) indicates that the process Exits normally. 1 is returned;

Exit (0) indicates that the process exits abnormally. 0 is returned.

Process environment and Process Control (1): Process initiation and Termination

1. process start:

The C program is executed from the main function. The prototype is as follows:

Int main (int argc, char * argv []);

Generally, the return value of main is int type, and 0 is returned correctly.

If the return value of main is void or none, Some compilers will give a warning. In this case, the return value of main is usually 0.

The main command line parameters are not explained too much. The following program shows:


The following is a code snippet:
# Include
Int main (INT argc, char * argv [])
Int I;
For (I = 0; I <argc; I ++)
Printf ("argv [% d]: % s/n", I, argv [I]);
Return 0;


2. Process Termination:

There are two types of termination of C program: normal termination and exceptional termination.

Normal termination is divided into: return, exit, _ exit, _ Exit, pthreade_exit

Exception: abort, SIGNAL, thread response canceled

It mainly refers to the first four types of normally terminated functions, namely exit functions.


The following is a code snippet:
# Include/* iso c */
Void exit (int status );
Void _ Exit (int status );
# Include/* POSIX */
Void _ exit (int status );


The differences between the above three functions are:

Exit () (or return 0) will call the standard I/O cleanup programs (such as fclose) that terminate the processing program and the user space ), _ exit and _ Exit are cleared directly by the kernel instead of being called.


Therefore, exit (0) in the main function is equivalent to return 0.

3. atexit:

Iso c requires that a process can register up to 32 Termination handler functions, which are automatically called by exit in the reverse order of registration. If the same function is registered multiple times

Multiple calls.

The prototype is as follows:

# Include

Int atexit (void (* func) (void ));

The parameter is a function pointer pointing to the end processing function. This function has no parameters and no return value.

The following procedure is used as an example:


The following is a code snippet:
# Include
Static void myexit1 ()
Printf ("first exit handler/n ");
Static void myexit2 ()
Printf ("second exit handler/n ");
Int main ()
If (atexit (my_exit2 )! = 0)
Printf ("can't register my_exit2/n ");
If (atexit (my_exit1 )! = 0)
Printf ("can't register my_exit1/n ");
If (atexit (my_exit1 )! = 0)
Printf ("can't register my_exit1/N ");
Printf ("Main is done/N ");
Return 0;


Running result:


The following is a code snippet:
$./A. Out
Main is done
First exit Handler
First exit Handler
Second exit handler running result:
$./A. Out arg1 arg2 arg3
Argv [0]:./A. Out
Argv [1]: arg1
Argv [2]: arg2
Argv [3]: arg3



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: 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.