exit() in C

The C exit() function is a standard library function used to terminate the calling process. When exit() is called, any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal.

It is defined inside the <stdlib.h> header file.

Syntax of exit() in C

void exit(int status);

Parameters

The exit() function in C only takes a single parameter status which is the exit code that is returned to the caller i.e. either the operating system or parent process. There are two valid values we can use as the status each having a different meaning. They are as follows:

  • 0 or EXIT_SUCCESS: 0 or EXIT_SUCCESS means that the program has been successfully executed without encountering any error.
  • 1 or EXIT_FAILURE: 1 or EXIT_FAILURE means that the program has encountered an error and could be executed successfully.

Note: We can actually return any non-zero return value in case of failure.

Return Value

  • This function doesn’t return any value to the current process so its return type is void. Instead, it returns the status value to the parent process which is returned through a method different than the return value.

Example 1:  C Program to Illustrate the exit() Function

C




// C Program to demonstrate the syntax of exit() function
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    FILE* pFile;
    pFile = fopen("myfile.txt", "r");
 
    if (pFile == NULL) {
        printf("Error opening file");
 
        // terminating the process if the file is not opened
        exit(1);
    }
    else {
        /* file operations here */
    }
    return 0;
}


Output

Error opening file

How exit() in C works?

When called, the exit() function in C performs the following operations:

  1. Flushes unwritten buffered data.
  2. Closes all open files.
  3. Removes temporary files.
  4. Returns an integer exit status to the operating system.

Example 2: Passing an integer value greater than 255 to the exit() function in C

C




// C Program to verify the status value
// size of the exit() function
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
 
int main(void)
{
    pid_t pid = fork();
 
    if (pid == 0) {
        // passing value more than 255
        exit(9999);
    }
 
    int status;
    waitpid(pid, &status, 0);
 
    if (WIFEXITED(status)) {
        int exit_status = WEXITSTATUS(status);
 
        printf("Exit code: %d\n", exit_status);
    }
 
    return 0;
}


Output

Exit code: 15

Explanation

In the above function, instead of 9999, the status value is 15. It is an effect of 8-bit integer overflow. After 255 (all 8 bits set) comes 0. As the exit() supports only 8-bit integer values, the output is “exit code modulo 256”. The output above is actually the modulo of the value 9999 and 256 i.e. 15.

The C standard atexit() function can be used to customize exit() to perform additional actions at program termination.

C exit(), abort() and assert() Functions

The C exit(), abort(), and assert() functions are all used to terminate a C program but each of them works differently from the other. In this article, we will learn about exit, abort, and assert functions and their use in C programming language.

Similar Reads

1. exit() in C

The C exit() function is a standard library function used to terminate the calling process. When exit() is called, any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal....

2. abort() in C

...

3. assert() in C

...

Contact Us