exit(0) vs exit(1) in C/C++ with Examples - GeeksforGeeks

16 Jan, 2020
exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status.
There are two types of exit status in C/C++:
Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // opening the file in read-only mode    file = fopen("myFile.txt", "r");      printf("File opening successful!");      // EXIT_SUCCESS    exit(0);}Note: Create a file called ‘myFile.txt’ and run the code in your local device to see the output.Exit Failure: Exit Failure is indicated by exit(1) which means the abnormal termination of the program, i.e. some error or interrupt has occurred. We can use different integer other than 1 to indicate different types of errors.#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // open the file in read-only mode    file = fopen("myFile.txt", "r");      if (file == NULL) {        printf("Error in opening file");          // EXIT_FAILURE        exit(1);    }      // EXIT_SUCCESS    exit(0);}
Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // opening the file in read-only mode    file = fopen("myFile.txt", "r");      printf("File opening successful!");      // EXIT_SUCCESS    exit(0);}Note: Create a file called ‘myFile.txt’ and run the code in your local device to see the output.
#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // opening the file in read-only mode    file = fopen("myFile.txt", "r");      printf("File opening successful!");      // EXIT_SUCCESS    exit(0);}
Note: Create a file called ‘myFile.txt’ and run the code in your local device to see the output.
Exit Failure: Exit Failure is indicated by exit(1) which means the abnormal termination of the program, i.e. some error or interrupt has occurred. We can use different integer other than 1 to indicate different types of errors.#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // open the file in read-only mode    file = fopen("myFile.txt", "r");      if (file == NULL) {        printf("Error in opening file");          // EXIT_FAILURE        exit(1);    }      // EXIT_SUCCESS    exit(0);}
#include <file.h>#include <stdio.h>  int main(){    FILE* file;      // open the file in read-only mode    file = fopen("myFile.txt", "r");      if (file == NULL) {        printf("Error in opening file");          // EXIT_FAILURE        exit(1);    }      // EXIT_SUCCESS    exit(0);}
Let’s see the differences between these two statements-
C Language
C++
Difference Between
Technical Scripter
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Left Shift and Right Shift Operators in C/C++
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects