c ++ exit () function

Idiom

edit

Function name: exit ()
Where the header file: stdlib.h (if it is "VC6.0" Buddist file: windows.h)
Function: Close all files, terminate the process is being executed.
exit (0) indicates a normal exit,
exit (x) (x is not 0) are represented quit unexpectedly, the x is returned to the operating system (including UNIX, Linux, and MS DOS), and for other programs.
stdlib.h: void exit (int status); // parameter status, the return value of the program to exit

Simple example program

edit
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
int  main( int  argc, char *argv[])
{
int  status;
printf ( "Enter either 1 or 2\n" );
status=getch();
/*Sets DOS error level*/
exit (status- '0' );
/*Note:this line is never reached*/
return  0;
}
exit () and return the difference between:
Accordance with ANSI C, using the same in the main exit and return to the original call () () in effect.
But pay attention to here is the "first call." If the main () in a recursive program, exit () will still terminate the program; but will return
Transfer of control to the previous level of recursion, until that first level, the return will terminate the program at this time. Another difference between return and exit () of
In that, in addition to the function call exit even in the main () other than the (), which also terminates the program.
_exit () and exit the difference:
head File:
exit:#include<stdlib.h>
_exit:#include<unistd.h>
_exit () function: direct the process stops running, clear the memory space it uses, and the destruction of its various data structures in the kernel;
exit () function is made on the basis of a number of these packaged, prior to performing a number of procedures to quit.
exit () function is the biggest difference with the _exit () function is that the exit () function to open the file in case you want to check before calling the exit system call, the contents of the file buffer write the file.
NOTE: exit () is the exit status code parameter passed when the program exits, 0 represents a normal exit, the other represents a non-normal exit, usually with 1 or -1, and the standard C there EXIT_SUCCESS EXIT_FAILURE two macros, with exit (EXIT_SUCCESS);

Exit Procedure

Edit
  1. Call atexit () function registration (export function); calls all functions registered by its order of registration by ATEXIT opposite, which allows us to perform its own cleanup actions specified in the program terminates, for example, save the program status. information in a file, to unlock the lock on the shared database and so on.
  2.cleanup (); closes all open streams, which will lead to output all buffered write , delete all temporary files created by TMPFILE function [1]   .
  3. The last call _exit () function to terminate the process.
  _exit do three things:
  1, file descriptors belonging to the process will be closed
2, any child process inherits the process by process 1
Parent node 3, the process of sending a signal SIGCHLD
After the exit after executing cleanup call _exit to terminate the process.
//==========================================================EX

exit (0): normal operation of the program and exit the program;

exit (1): abnormal operation leading to exit the program;

return (): function that returns, if the main function, the function exits and will return a value.

Detail:

  1. return function return value is a keyword; exit is a function.

  2. return is the language level, which represents a return call stack; and exit system call level, it represents the end of a process.
  3. return is the exit function (returns); exit is the exit process.

  4. return is provided by the C language, exit is provided by the operating system (or a given library).

  5. return for ending the execution of a function, the function execution information outgoing calls other functions use; exit function is to exit the application, a state of the memory space using the delete process, and the application returns to the OS, the state identification information to run some applications, and the information machines and operating systems, generally is 0 for normal exit non-zero non-normal exit.

  6. Non main function call return and exit effect is obvious, but the call of the phenomenon of return and exit is very vague in the main function, the phenomenon in many cases are the same.

 

exit (0) and exit (1) of your program, there is no difference. Of people using your program or programs, the difference may become significant.
Generally, exit 0 can inform the user of your program: your program is completed normally. If the exit non-zero value, then the user of your program will usually think that your program generates an error.

In shell, for example, after you complete the procedure call in the shell with echo $? Command you can see the exit value of your program. In shell scripts, usually based on a $ orders? Value to some process control.

Guess you like

Origin www.cnblogs.com/YZFHKMS-X/p/11755957.html