exit function-exit the program

Use the exit function to exit the program. It can be used by including the stdlib.h header.

void exit (int status);

Specify "0" for the argument to mean normal termination, and specify "non-zero" to mean abnormal termination. In the case of abnormal termination, it seems that "1" is customarily specified.

Sample to display an error message and exit the program

This is a sample that displays an error message on the standard error output with the fprintf function and terminates the program with the exit function. The error message includes the file name and line number.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  
  // An error has occured
  int32_t error = 1;
  
  if (error) {
    fprintf (stderr, "Error at%s line%d\n", __FILE__, __LINE__);
    exit (1);
  }
}

Associated Information