The exit(0) and exit(1) are the jump statements of C++ that make the control jump out of a program while the program is in execution. Both the functions, exit(0) and exit(1), are used to exit out of the program, but there is one major difference between exit(0) and exit(1). The exit (0) shows the successful termination of the program and the exit(1) shows the abnormal termination of the program.
Let’s study the difference between exit(0) and exit(1) with the help of a comparison chart.
Content: exit(0) Vs exit(1)
Comparison chart
Basis For Comparison | exit(0) | exit(1) |
---|---|---|
Basic | Reports the operating system about the "successful/normal" termination/completion of the program. | Reports the operating system about the "abnormal" termination of the program. |
Syntax | exit(0); | exit(1); |
Indicates | It indicates that the task has been successfully performed. | It indicates that the task has been aborted in between because of the error. |
Macros | EXIT_SUCCESS | EXIT_FAILURE |
Definition of exit(0)
The function exit(0) is a jump statement of C++. It is used to terminate the program or let the control exit out of the program. It reports the operating system about the successful termination of the program which indicates to the operating system that the task of the program has been successfully completed.
The macro used for return code “0” is “EXIT_SUCCESS”, so, you can use it in a way exit(EXIT_SUCCESS). The general form of the exit(0) function is:-
void exit(int return_code);
Here, the formal parameter “return_code” is the value that is returned to the calling function. The returen_code is always of integer type as the value returned to the calling function will either be zero or a non-zero value. The exit(0) is a standard library function, if we are using exit(0) in the program we have to use the header file <cstdlib.h>.
Let us understand exit(0) with an example:-
#include <stdlib.h> // standard library function int main () { FILE * ptrFile; ptrFile = fopen ("myfile.txt","r"); // open the file in read only mode if (ptrFile==NULL) { cout<<"Error in opening file"; exit (1); //alternately you can use exit(EXIT_FAILURE) } exit (0); //alternately you can use exit(EXIT_SUCCESS) }
In the code above we are trying to open a file named “myfile.txt”. We had created a pointer to the file “myfile.txt”. If the file “myfile.txt” does exist, the pointer will point to the address of that file and exit(0) will execute reporting the operating system that the file is successfully opened.
In case the file is not present the pointer to the file “myfile.txt” will now contain NULL and exit(1) will get execute reporting the operating system that the file does not open due to error or something.
Definition of exit(1)
The function exit(1) is also a jump statement of C++. The exit(1) also terminates the program but, abnormally. The exit(1) reports the operating system that the program is not successfully executed, or it is aborted in between the execution due to some or the other error. The exit(1) function is defined in the standard library function, in case you are using exit(1) in your program you have to specifically mention the header file <cstdlib.h> at the top of the program.
The macro for return code “1” is “EXIT_FAILURE”, so, it can be written in a way “exit(EXIT_FAILURE)”. Now let us understand the exit(1) function with the help of the program.
//pop the element at the top of the stack int pop(int stack_name, int size, int Top){ if (Top==-1){ cout<<"stack is underflow"; exit(1); } else { int s=s[Top]; Top--; return (s); } }
Here, the function is defined to pop the element at the top of the stack, if the top of the stack is found to be empty i.e. the Top is -1. Then the task of popping out the topmost element in the stack is not completed successfully as the stack is empty then we return exit(1). It indicates that the task of the pop function has not been completed. Hence, the execution is terminated abnormally.
Key Differences Between exit(0) and exit(1)
- The only return_code that indicate the successful termination of the program is “0”. For reporting abnormal termination of the program, we can use any value other than “0” i.e. we can use “1”, “2”, “3”… that means a nonzero value indicates abnormal termination of the program.
- A Macro can also be used instead of the return_code. Like, in place “0” you can use “EXIT_SUCCESS” whereas in place of “1” you can use “EXIT_FAILURE”.
Similarity
- Both exit(0) and exit(1), are the jump statements of C++.
- Both exit(0) and exit(1), are used to terminate the program.
- Both exit(0) and exit(1), are definedunder the header file<cstdlib.h>.
- Both exit(0) and exit(1), report the status of termination of the program to the operating system.
Note
If the exit( ) function does not return anything, it means it does not want to reveal the status of the termination of the program to the operating system.
Conclusion
To report the status of the termination of the program, one uses an exit( ) function. An exit(0) reveal to the operating system that the task of the program has been successfully completed. An exit(1) reveals that the task of the program is not completed, and the program execution is aborted abnormally.
Leave a Reply