C programming stdlib.h function - void exit(int status)

The exit function is a standard library function in C that terminates the calling process. The exit function takes an integer argument, which is the exit status of the process.

The syntax of the exit function is as follows:

void exit(int status);
Source:w‮ww‬.theitroad.com

The exit function terminates the calling process, flushing any buffered output and closing any open files. The integer argument status is the exit status of the process. A status of 0 indicates successful termination of the process, while any other value indicates an error.

Here is an example that demonstrates how to use the exit function:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    printf("Before exit.\n");
    exit(0);
    printf("After exit.\n");
    return 0;
}

In this example, the exit function is called with a status of 0, which indicates successful termination of the process. The program prints "Before exit." to the console, calls the exit function, and then exits without printing "After exit." to the console.

Note that the exit function is typically used to terminate a process in response to an error condition. When an error occurs, the process can call the exit function with a nonzero exit status to indicate the error. This exit status can be used by other processes that are monitoring the process, to determine the cause of the termination.