The exit() function in C
The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.
Important points of the exit() function
Following are the main points of the exit function in C programming as follows:
Syntax of the exit() function
The exit() function has no return type.
int status: It represents the status value of the exit function returned to the parent process.
Example 1: Program to use the exit() function in the for loop
Let's create a program to demonstrate the exit (0) function for normal terminating the process in the C programming language.
Enter the last number: 10 Number is 1 Number is 2 Number is 3 Number is 4 Number is 5
There are two types of exit status in C
Following are the types of the exit function in the C programming language, as follows:
EXIT_SUCCESS: The EXIT_ SUCCESS is the exit() function type, which is represented by the exit(0) statement. Where the '0' represents the successful termination of the program without any error, or programming failure occurs during the program's execution.
Syntax of the EXIT SUCCESS
Example 1: Program to demonstrate the usage of the EXIT_SUCCESS or exit(0) function
Let's create a simple program to demonstrate the working of the exit(0) function in C programming.
Start the execution of the program. Exit from the program.
Example 2: Program to use the EXIT_SUCCESS macro in the exit() function
Let's create a C program to validate the whether the character is presents or not.
Enter the character: Y Great, you did it.
EXIT_FAILURE: The EXIT_FAILURE is the macro of the exit() function to abnormally execute and terminate the program. The EXIT_FAILURE is also represented as the exit(1) function. Whether the '1' represents the abnormally terminates the program and transfer the control to the operating system.
Syntax of the EXIT_FAILURE
Example 1: Let's create a program to use the EXIT_FAILURE or exit(1) function
Enter the num1: 20 Enter the num2: 6 20 / 6 : 3.333333 2nd Run Enter the num1: 20 Enter the num2: 6 Dividend cannot be zero
Example 2: Let's create another program to use the EXIT_FAILURE to terminate the C program.
Unable to open the defined file.