exit() vs _Exit() in C/C++


exit()

The function exit() is used to terminate the calling function immediately without executing further processes. As exit() function calls, it terminates processes. It calls the constructor of class only. It is declared in “stdlib.h” header file in C language. It does not return anything.

The following is the syntax of exit()

void exit(int status_value);

Here,

status_value − The value which is returned to parent process.

The following is an example of exit()

Example

 Live Demo

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10;
   printf("The value of x : %d\n", x);
   exit(0);
   printf("Calling of exit()");
   return 0;
}

Output

The value of x : 10

In the above program, a variable ‘x’ is initialized with a value. The value of variable is printed and exit() function is called. As exit() is called, it exits the execution immediately and it does not print the statement in the printf(). The calling of exit() is as follows −

int x = 10;
printf("The value of x : %d\n", x);
exit(0);

_Exit()

The function _Exit() is used to terminate the process normally and it returns the control to host environment. It does not perform any cleanup task.

The following is the syntax of _Exit()

void _Exit(int status_value);

Here,

status_value − The value which is returned to parent process.

The following is an example of _Exit()

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10;
   printf("The value of x : %d\n", x);
   _Exit(0);
   printf("Calling of _Exit()");
   return 0;
}

In the above program, neither it will display anything nor it will show error.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

468 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements