Idiom
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 Procedure
Edit1. 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
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.