Exit 0 in C
Introduction
Exit() is a function, not a keyword, in C. We invoke a method whenever we call exit(). The exit() function used in C programming ends a program and gives the operating system an exit status.
In C, the jump statements that cause the control element to depart the program are the exit(0) and exit(1) commands. The stdlib.h header file contains the exit() function in the C programming language. There are two different sorts of exit states in C:
- Exit(0): Exit(0) signifies a successful exit.
- Exit(1): Exit(1) indicates an exit failure.
What is exit(0) in C?
Exit(0) is a function call in C that ends the currently executing program and sends a status of 0 to the operating system. The exit status is important since it shows if the program has been completely stopped or has encountered errors. The exit(0) statement indicates that the program has been successfully terminated, which indicates it has run without hitting any errors. It informs the operating system that the program was successfully terminated and error-free. This tells the operating system that the program was a success.
The Syntax for Exit 0 is as follows:
exit(0);
The return value of the exit(0) function is void, i.e., null.
"EXIT_SUCESS" is the macro used for return code 0.
Examples for exit(0)
Example 1
The following is the code for exit 0:
#include<stdlib.h>
#include<stdio.h>
int main(){
printf("Example code for exit 0\n");
exit(0); //EXIT_SUCCESS
}
Output
In the above example, the main() method will output "Example code for exit 0" to the console and then call the function exit(0) to end the program using a status code of 0, which denotes the effective execution of the program.
Example 2
Code
#include<stdlib.h>
#include<stdio.h>
int main(){
intnum;
printf("Enter the value of num: ");
scanf("%d",&num);
for(inti=0;i<=num;i++){
if(i==num-2){
exit(0);
}
else{
printf("%d\n",num+i);
}
}
return 0;
}
Output
In the example above, the program is terminated with a status code of 0, which denotes that it was effectively performed once the value of 'i' increases to 3. This is done by calling the exit(0) function.
What is exit(1) in C?
“Exit(1) is a function call in C that ends the currently executing program but unexpectedly exits with status 1 to the operating system.”
The operating system is notified whenever an anomalous ending occurs due to an interruption or error using the exit(1) instruction.
The Syntax for exit(1) is as follows:
exit(1);
The function exit(1) returns void.
"EXIT_FAILURE" is the macro that is used for return code 1.
Example for exit(1)
The following is the example code for exit(1)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
double t = -20.0;
double answer= 0.0;
if (t < 0){
printf("Error: Temperature(t) is a negative value.\n");
exit(1);
}
answer = sqrt(t); // using built-in function to calculate square root
printf("The square root of %lf is %lf.\n", t, answer);
return 0;
}
Output
The program will perform A square root calculation in the example above.
- The program first determines whether the number contained in the variable "t" is negative before calculating the square root.
- The program will produce an error message and use the exit(1) function to end itself if the value of 't' is negative.
- The program employs the built-in function from the math library called "sqrt" to calculate the square root of "t" if it is not negative.
- The outcome will be printed by the program at the end.
Differences between exit(0) and exit(1)
exit(0)
- It is portable.
- It provides information on how the program was successfully ended or finished.
- When the program runs flawlessly, it informs the user of the completion.
- The ‘EXIT_SUCCESS’ macro is used to return code 0.
- One can specify the 'EXIT_SUCCESS' as being always zero.
exit(1)
- It is not portable.
- It provides details regarding the unexpected program termination.
- If a program exited while running with a specific error, it will inform the user of the termination.
- The ‘EXIT_FAILURE’ macro is used to return code 1.
- The requirement that it be 1 is not a restriction.
- The system's implementation will determine the way it can be used.
Similarities between exit(0) and exit(1)
- The operating system regains control once both functions finish the program.
- A number identifying the program's status upon completion is supplied as an argument to exit(0) and exit(1).
- Jump statements are a feature of the C language, and exit(0) and exit(1) are both.
- Both of these functions are a part of the C standard library and are present in any program that uses the stdlib.h header file.
Conclusion
An exit() function is used to report the status of the program's end. The operating system can be informed that the program's purpose was finished effectively by an
exit(0). The program's work has not been accomplished, is shown by an exit(1), and the execution of the program has been terminated unexpectedly.