C++ Standard Library C++ STL Library

C++ <cstdlib> - EXIT_FAILURE constant



The C++ <cstdlib> EXIT_FAILURE is a macro constant that expands to an integer expression that can be used as arguments to the exit function and indicates that the application has failed.

The opposite meaning can be specified with EXIT_SUCCESS.

In the <cstdlib> header file, it is defined as follows:

#define EXIT_FAILURE /*implementation defined*/          

Example:

Consider the example below where the program tries to open the file test.txt. The program fails to open this file and after that it prints the message and exits using exit(EXIT_FAILURE) statement.

#include <cstdio>
#include <cstdlib>
 
int main (){
  //open the file in read mode
  FILE *pFile = fopen("test.txt", "r");

  if (pFile == NULL){
    printf("Error opening file");
    exit(EXIT_FAILURE);
  } else {
    printf("File is opened successfully."); 
    exit(EXIT_SUCCESS);  
  }

  printf("This line is not executed."); 
 
  return EXIT_SUCCESS;
}

The output of the above code could be:

Error opening file

❮ C++ <cstdlib> Library

5