Program Termination

There are several ways to exit a program:

Call the exit function.

Call the abort function.

Fail an assert test.

Execute a return statement from main.

exit Function

The exit function, declared in the standard include file STDLIB.H, terminates a C++ program.

The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully.

Note:

You can use the constants EXIT_FAILURE and EXIT_SUCCESS, defined in STDLIB.H, to indicate success or failure of your program.

Issuing a return statement from the main function is equivalent to calling the exit function with the return value as its argument.

abort Function

The abort function, also declared in the standard include file STDLIB.H, terminates a C++ program. The difference between exit and abort is that exit allows the C run-time termination processing to take place, and abort causes immediate program termination.

assert Macro

The assert macro allows programmers to insert conditional failure code inline to assist in debugging. Should the programmer's “assertion” prove false, the location of the assertion is printed on the standard output device and the program terminates.

See the Run-Time Library Reference manual for more information about using exit, abort, and assert to terminate program execution.

return Statement

Issuing a return statement from main is functionally equivalent to calling the exit function. Consider the following example:

int main()

{

exit( 3 );

return 3;

}

The exit and return statements in the preceding example are functionally identical. However, C++ requires that functions that have return types other than void return a value. The return statement allows you to return a value from main.