Difference between exit(0) and exit(1) in C
In this article, we will discuss the difference between exit(0) and exit(1) in C. But before discussing differences, we must know about the exit() function.
What is the exit() function?
A program can be stopped using the exit() method from the standard library. It accepts an integer input that describes the program's exit state. The operating system receives the exit status back and can use it to figure out how the program ended.
A program is normally ended with the exit() method when an error occurs or after it has finished its task. If a user enters an invalid number, the program will end.
Example:
#include <stdio.h> #include <stdlib.h> // Include the necessary header for the exit() function int main() { int userNumber; // Change variable name from 'number' to 'userNumber' printf("Enter a number: "); scanf("%d", &userNumber); if (userNumber < 0) { printf("Error: Entered number is less than 0\n"); exit(1); // Exit with an error code of 1 } printf("The number is %d.\n", userNumber); return 0; }
Output:
Explanation:
In this instance, if the user inputs a value that is less than 0, the exit() method is activated. The exit() function's parameter is 1, which denotes that an error caused the program to terminate. In C, the exit() method plays a crucial role. Programs are terminated cleanly, and the operating system is provided with a status code.
When a program exits, it returns an integer value known as an exit status code. The operating system normally uses the exit status code to ascertain how the program exited.
While non-zero values (like 1) normally denote an error or abnormal termination, status codes of 0 typically indicate a successful execution. A status code of 1 might mean that the program ran out of memory, whereas a status code of 2 might mean that a segmentation error was occurred.
The program that generated it often specifies an exit status code’s precise meaning. Nevertheless, a few standard exit status codes are frequently employed. Examples of typical exit status codes include the following:
0: Successful execution
1: General error
2: Syntax error
3: Undefined symbol
4: Segmentation fault
5: Illegal instruction
What is the exit(0) statement?
The exit(0) statement, a standard library function, is used to end a program and provide the operating system a status code of 0. Usually, a status code of 0 denotes a successful execution.
The exit(0) statement is frequently used when a program has successfully completed a task. For instance, the code that follows ends a program after printing the phrase "Namaste, world!".
Example:
#include <stdio.h> int main() { printf("Namaste, world!\n"); exit(0); }
Output:
Explanation:
In this illustration, the printf() function is run first, and then the exit(0) instruction is executed. The exit() function's parameter is 0, which means the program finished correctly.
In C, the exit(0) statement performs a crucial role. Programs are terminated cleanly, and the operating system receives a status code of 0. It is customary to use the exit(0) statement when a program has:
#include <stdio.h> #include <stdlib.h> // Include the necessary header for the exit() function int main() { printf("Hello, world!\n"); exit(0); // Explicitly exit with status 0 to indicate successful program execution }
Output:
What is the exit(1) statement?
The exit() function serves the purpose of concluding the execution of a program and conveying a status code to the operating system. This status code is an integer value, where the convention is to employ 0 to signify that the program has executed successfully. Conversely, non-zero values are typically used to signal errors or unusual program terminations. When you use exit(1), you are explicitly indicating that the program is terminating with an error status, making it clear to external processes or scripts that an issue has occurred during the program's execution.
Example:
#include <stdio.h> #include <stdlib.h> // Include the necessary header for the exit() function int main() { int userNumber; // Changed variable name 'number' to 'userNumber' printf("Enter a number: "); scanf("%d", &userNumber); if (userNumber < 0) { exit(1); } printf("The number is %d.\n", userNumber); return 0; }
Output:
The status code returned to the operating system is the primary distinction between exit(0) and exit(1) in C. A successful execution is often indicated by the status code 0, which exit(0) returns. The status code 1 returned by exit(1) often denotes an error or unusual termination.
The differences between exit(0) and exit(1) :
Function | Status Code | Description |
exit(0) | 0 | It indicates a successful execution. |
exit(1) | 1 | It indicates an error or abnormal termination. |