C++ _Exit()

The _Exit() function in C++ causes normal termination of a process without performing any regular cleanup tasks.

Neither any object destructors nor the functions registered by atexit or at_quick_exit are called. Whether open resources such as files are closed is implementation defined.

If the exit_code is 0 or EXIT_SUCCESS, a successful termination status is returned to the host environment.

If exit_code is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment. In other cases implementation-defined status value is returned.

_Exit() prototype

void _Exit(int exit_code);

The function is defined in <cstdlib> header file.


_Exit() Parameters

exit_code: An integer value representing the exit status of the program.

  • If exit_code is zero or EXIT_SUCCESS, it indicates successful termination.
  • If exit_code is non-zero or EXIT_FAILURE, it indicates failure.

_Exit() Return value

The _Exit() function returns nothing.


Example : How _Exit() function works?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int exit_code;
	cout << "Enter a value: ";
	cin >> exit_code;

	if (exit_code)
	{
		cout << "Exiting using _Exit";
		_Exit(exit_code);
	}
	else
	{
		cout << "Exiting using exit";
		exit(exit_code);
	}
}

When you run the program, the output will be:

Enter a value: 5
Exiting using _Exit