C++ Standard Library C++ STL Library

C++ <cstdlib> - EXIT_SUCCESS constant



The C++ <cstdlib> EXIT_SUCCESS 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 was successful.

The opposite meaning can be specified with EXIT_FAILURE.

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

#define EXIT_SUCCESS /* implementation defined */          

Example:

Consider the example below where the program tries to open the file test.txt. The program opens the file successfully and after that it prints the message and exits using exit(EXIT_SUCCESS) 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_SUCCESS);
  } 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:

File is opened successfully.

❮ C++ <cstdlib> Library