exit() Function
The exit()
function terminates the calling process, performing all the necessary cleanup tasks before returning control to the host environment. It provides a controlled way to end program execution, ensuring that resources are properly released.
Syntax of exit()
void exit(int status);
Parameters
Parameter | Description |
---|---|
status | Status code indicating the reason for termination. A status of 0 or EXIT_SUCCESS signifies a successful termination, whereas EXIT_FAILURE indicates an error. Other values may be returned based on the system and library implementation. |
When exit()
is invoked, normal program termination occurs. This involves destroying objects with static storage duration, calling functions registered with atexit()
, closing all open C streams (and flushing them if buffered), and removing temporary files created with tmpfile()
. Note that objects with automatic storage duration are not destroyed by this function.
Return Value
The function does not return any value.
Examples for exit()
Example 1: Exiting with a Successful Termination
This example demonstrates how to use exit()
to terminate a program successfully after completing its tasks.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Program is running...\n");
// Terminate the program with a successful status
exit(0);
// This line will never be executed
printf("This will not be printed.\n");
return 0;
}
Explanation:
- The program prints a message indicating that it is running.
exit()
is called with a status code of 0, indicating a successful termination.- The program terminates immediately, so any code after the
exit()
call is not executed.
Program Output:
Program is running...
Example 2: Exiting with a Failure Status on Error
This example shows how to use exit()
to terminate a program when an error occurs, providing a failure status to the host environment.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
fprintf(stderr, "An error has occurred!\n");
// Terminate the program with a failure status
exit(EXIT_FAILURE);
// This line will never be executed
printf("This will not be printed.\n");
return 0;
}
Explanation:
- The program prints an error message to the standard error stream.
exit()
is called withEXIT_FAILURE
to indicate unsuccessful termination.- The program terminates immediately, and no further code is executed.
Program Output:
An error has occurred!
Example 3: Executing Cleanup Functions Registered with atexit()
This example illustrates how cleanup functions registered with atexit()
are executed when exit()
is called. It ensures that all cleanup tasks are performed before the program terminates.
Program
#include <stdio.h>
#include <stdlib.h>
void cleanup(void) {
printf("Cleanup function executed.\n");
}
int main() {
// Register the cleanup function
atexit(cleanup);
printf("Performing tasks...\n");
// Terminate the program with a successful status
exit(EXIT_SUCCESS);
// This line will not be executed
printf("This will not be printed.\n");
return 0;
}
Explanation:
- A cleanup function
cleanup()
is defined to perform necessary cleanup tasks. - This function is registered using
atexit()
, ensuring it is called upon program termination. - The program prints a message indicating that tasks are being performed.
exit()
is called withEXIT_SUCCESS
, triggering the registered cleanup function before terminating the program.
Program Output:
Performing tasks...
Cleanup function executed.