exit() — End Program

Format

#include <stdlib.h>
void exit(int status);

Language Level: ANSI

Threadsafe: Yes.

Description

The exit() function returns control to the host environment from the program. It first calls all functions that are registered with the atexit() function, in reverse order; that is, the last one that is registered is the first one called. It deletes all buffers and closes all open files before ending the program.

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.

Note:
When compiled with SYSIFCOPT(*ASYNCSIGNAL), exit() cannot be called in a signal handler.

Return Value

The exit() function returns both control and the value of status to the operating system.

Example that uses exit()

This example ends the program after deleting buffers and closing any open files if it cannot open the file myfile.

#include <stdio.h>
#include <stdlib.h>
 
FILE *stream;
 
int main(void)
{
   if ((stream = fopen("mylib/myfile", "r")) == NULL)
   {
      perror("Could not open data file");
      exit(EXIT_FAILURE);
   }
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]