Javatpoint Logo
Javatpoint Logo

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.

The exit() function in C

Important points of the exit() function

Following are the main points of the exit function in C programming as follows:

  1. We must include the stdlib.h header file while using the exit () function.
  2. It is used to terminate the normal execution of the program while encountered the exit () function.
  3. The exit () function calls the registered atexit() function in the reverse order of their registration.
  4. We can use the exit() function to flush or clean all open stream data like read or write with unwritten buffered data.
  5. It closed all opened files linked with a parent or another function or file and can remove all files created by the tmpfile function.
  6. The program's behaviour is undefined if the user calls the exit function more than one time or calls the exit and quick_exit function.
  7. The exit function is categorized into two parts: exit(0) and exit(1).

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.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA