20 Apr 2017

Difference Between exit(0) and exit(1)


exito and exit 1The 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 study difference between exit(0) and exit(1) with the help of comparison chart.

Content: exit(0) Vs exit(1)

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison chart

BASIS FOR COMPARISONEXIT(0)EXIT(1)
BasicReports the operating system about the "successful/normal" termination/completion of the program.Reports the operating system about the "abnormal" termination of the program.
Syntaxexit(0);exit(1);
IndicatesIt indicates that the task has been successfully performed.It indicates that the task has been aborted in between because of the error.
MacrosEXIT_SUCCESSEXIT_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:-
  1. 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:-
  1. #include <stdlib.h> // standard library function
  2. int main ()
  3. {
  4. FILE * ptrFile;
  5. ptrFile = fopen ("myfile.txt","r"); // open the file in read only mode
  6. if (ptrFile==NULL)
  7. {
  8. cout<<"Error in opening file";
  9. exit (1); //alternately you can use exit(EXIT_FAILURE)
  10. }
  11. exit (0); //alternately you can use exit(EXIT_SUCCESS)
  12. }
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.
  1. //pop the element at the top of the stack
  2. int pop(int stack_name, int size, int Top){
  3. if (Top==-1){
  4. cout<<"stack is underflow";
  5. exit(1);
  6. }
  7. else
  8. {
  9. int s=s[Top];
  10. Top--;
  11. return (s);
  12. }
  13. }
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 top most 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)

  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 indicate abnormal termination of the program.
  2. 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:

  1. Both exit(0) and exit(1), are the jump statements of C++.
  2. Both exit(0) and exit(1), are used to terminate the program.
  3. Both exit(0) and exit(1), are definedunder the header file<cstdlib.h>.
  4. Both exit(0) and exit(1), report the status of termination of the program to the operating system.

Note:

If the exit( ) function do 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.

No comments:

Post a Comment

commnet here