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>intmain(){FILE* file;// opening the file in read-only modefile =fopen("myFile.txt","r");printf("File opening successful!");// EXIT_SUCCESSexit(0);}chevron_rightfilter_noneNote: 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>intmain(){FILE* file;// open the file in read-only modefile =fopen("myFile.txt","r");if(file == NULL) {printf("Error in opening file");// EXIT_FAILUREexit(1);}// EXIT_SUCCESSexit(0);}chevron_rightfilter_none
Let’s see the differences between these two statements-
| exit(0) | exit(1) |
|---|---|
| Reports the successful termination/completion of the program. | Reports the abnormal termination of the program. |
| Reports the termination when the program gets executed without any error. | Reports the termination when some error or interruption occurs during the execution of the program. |
| The syntax is exit(0); | The syntax is exit(1); |
| The usage of exit(0) is fully portable. | The usage of exit(1) is not portable. |
| The macro used for return code 0 is EXIT_SUCCESS | The macro used for return code 1 is EXIT_FAILURE |
| EXIT_SUCCESS is defined by the standard to be zero. | EXIT_FAILURE is not restricted by the standard to be one, but many systems do implement it as one. |
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference Between malloc() and calloc() with Examples
- Understanding "volatile" qualifier in C | Set 2 (Examples)
- isalpha() and isdigit() functions in C with cstring examples.
- Pointers in C/C++ with Examples
- strtok() and strtok_r() functions in C with examples
- memset() in C with examples
- std::mismatch() with examples in C++
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- select command in Linux with examples
- set_symmetric_difference in C++ with Examples
- df Command in Linux with examples
- ratio_equal() in C++ with examples
- fgrep command in Linux with examples
- std::equal_to in C++ with Examples
- quick_exit() function in C++ with Examples
- Java Math sin() method with Examples
- Java Math sinh() method with Examples
- Java Math cosh() method with Examples
- Java Math tanh() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

