Use of exit function in C/C++ with Examples

The exit function terminates the process normally also it performs the regular cleanup for terminating programs. It defined in the ‘stdlib.h’ header file, so you have to include the header file before using it.

When the exit function invokes it performs several cleanup operations, these are the following.

  • Call the functions registered by the atexit function, in the reverse order of their registration.
  • Flushed all open streams with unwritten buffered data.
  • Close all open streams, and removed all files created by the tmpfile function.
  • Finally, control is returned to the host environment.
    • If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
    • If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned.
    • In other cases, the implementation-defined status value is returned.

 

Syntax exit in C:

//Syntax of exit in c

void exit(int status);  (until C11)


_Noreturn void exit(int status);  (since C11)

Parameters:

status: Indicates whether the program terminated normally. It can be one of the following:

Value Description
EXIT_SUCCESS Successful termination
0 Successful termination
EXIT_FAILURE Unsuccessful termination

Return:

The exit function cannot return to its caller.

 

Let’s see an example code to understand the exit function in C. This example code verifies the successful opening of the file aticleworld.txt. If an error occurs, an error message is printed, and the program ends with a call to the exit() function.

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

int main()
{
    FILE *fptr = fopen("aticleworld.txt","r");
    if (fptr == NULL)
    {
        fprintf(stderr, "Failed to open the file\n");
        exit (EXIT_FAILURE);
    }

    /* Normal processing continues here. */
    fclose(fptr);
    printf("Normal Return\n");

    return 0;
}

Output:

Let us compile and run the above program that will produce the following result when it tries to open the aticleworld.txt file, which does not exist,

exit function in c

 

Some important points related to the exit() function in C:

1. You must include stdlib.h header file before using the abort function in C.

2. It calls the functions registered by the atexit function, in the reverse order of their registration. You can see the article “atexit in C“.

3. Flushed all open streams with unwritten buffered data.

4. Close all open streams, and removed all files created by the tmpfile function.

5. The functions registered with at_quick_exit are not called.

6. The behavior is undefined if a program calls exit more than once, or if it calls exit and quick_exit.

7. The behavior is undefined if, during a call to a function registered with atexit, the function exits with longjmp.

Difference between exit(EXIT_SUCCESS) and exit(EXIT_FAILURE):

Let’s see the differences between these two statements-
EXIT(EXIT_SUCCESS) EXIT(EXIT_FAILURE)
Reports the successful termination/completion of the program. Reports the abnormal termination of the program.
Reports the termination when the program gets executed without any error. Reports the termination when some error or interruption occurs during the execution of the program.
The syntax is exit(EXIT_SUCCESS); The syntax is exit(EXIT_FAILURE);
The usage of exit(EXIT_SUCCESS) is fully portable. The usage of exit(EXIT_FAILURE) is not portable.
EXIT_SUCCESS is defined by the standard to be zero. EXIT_FAILURE is not restricted by the standard to be one, but many systems do implement it as one.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *