6.19.16 exit Function
Terminates program after clean up.
Include
<stdlib.h>
Prototype
void exit(int status);
Argument
status
- the exit status to return
Remarks
The exit
function calls any functions registered by
atexit
in reverse order of registration, flushes buffers, closes
streams, closes any temporary files created with tmpfile
and then calls
_Exit
.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main()
{
double result, x=-9.0;
result = log(x);
if(errno) {
printf("terminating execution\n");
exit(EXIT_FAILURE);
} else
printf("Log of %g is %g\n", x, result);
}
Example Output
terminating execution