Syntax
#include <stdlib.h> /* also in <process.h> */ void exit(int status);Description
exit returns control to the host environment from the program. It first calls all functions registered with the atexit function, in reverse order; that is, the last one registered is the first one called. See atexit for more information. It flushes all buffers and closes all open files before ending the program. All files opened with tmpfile are deleted.
The argument status can have a value from 0 to 255 inclusive or be one of the macros EXIT_SUCCESS or EXIT_FAILURE. A status value of EXIT_SUCCESS or 0 indicates a normal exit; otherwise, another status value is returned.
exit returns both control and the value of status to the operating system.
This example ends the program after flushing buffers and closing any open files if it cannot open the file myfile.mjq.
#include <stdio.h> #include <stdlib.h> FILE *stream; int main(void) { if (NULL == (stream = fopen("myfile.mjq", "r"))) { perror("Could not open data file"); exit(EXIT_FAILURE); } return 0; /**************************************************************************** The output should be: Could not open data file: The file cannot be found. ****************************************************************************/ }Related Information