The use of the Exit function in C language

Source: Internet
Author: User
Tags exit in

Exit () End the current process/current program/, in the entire program, as long as the exit is called, the end

Return () is the current function return, of course, if it is in the main function main, it will naturally end the current process, if not, that is to return to the previous layer of the call. In multiple processes. If you sometimes want to detect if the process is exiting normally. The return value of the last process is used.

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

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

Process environment and Process Control (1): Start and end of process

1. Start of the process:

The C program starts from the main function and is prototyped as follows:

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

Normally the return value of main is int and returns 0 correctly.

If the return value of main is void or none, some compilers give a warning, at which point the return value of main is usually 0.

The command-line arguments about main do not explain too much, as shown in the following procedure:

here is the 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:

The termination of the C program is divided into two types: normal termination and abnormal termination.

Normal termination is divided into: return, exit, _exit, _exit, Pthreade_exit

Exception middle finger: Abort, SIGNAL, thread response canceled

Mainly about the normal termination of the first 4 kinds, that is, the Exit series function.

Here is the code snippet:
#include
void exit (int status);
void _exit (int status);
#include
void _exit (int status);

The differences between the above 3 functions are:

Exit () (or return 0) invokes a standard I/O cleaner (such as fclose) that terminates the handler and user space, and _exit and _exit are not called but are directly taken over by the kernel to clear

Acting.

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

3.atexit Termination Handler:

ISO C stipulates 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, it will also be

Called multiple times.

The prototype is as follows:

#include

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

Where the parameter is a function pointer, pointing to the terminating handler function, which has no parameter and no return value.

Take the following procedure as an example:

Here's the code snippet:
#include  
static void Myexit1 ()  

printf ("First exit handler\n");  

Static V OID Myexit2 ()  

printf ("Second exit handler\n");  

Int main ()  
{&nbs P
if (atexit (my_exit2)! = 0)  
printf ("Can ' t register my_exit2\n");  
if (atexit (my_exit1)! = 0) &nbs P
printf ("Can ' t register my_exit1\n");  
if (atexit (my_exit1)! = 0)  
printf ("Can ' t register my_exit 1\n ");  
printf (" Main is done\n ");  
return 0; 
}

Operation Result:

Here is the code snippet:

$./a.out
Main is done
First Exit Handler
First Exit Handler
Second exit handler run result:
$./a.out arg1 arg2 Arg3
Argv[0]:./a.out
ARGV[1]: arg1
ARGV[2]: arg2
ARGV[3]: Arg3

The use of the Exit function in C language

Related Article

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.