Exit program in C++

Exit program in C++

In this post, we will see how to exit program in C++.

This post will also cover following topics.

  • How to end program in C++.
  • How to close program in C++.

Exit program in C++

A program terminates when the final line of code executes. This also frees up the allocated resources. However, at times a need may arise on terminating a program when some condition is met. Like every other language, there are certain functionalities available to end a program in C++ on call.
Here are multiple ways to exit program in C++.

  • Using return statement
  • Using try and catch blocks
  • Using the exit() function
  • Using the _Exit() function
  • Using the quick_exit() function
  • Using the terminate() function

Ways to end program in C++

Let us now discuss different methods to exit a program in C++.

Using the return statement

In C++, the program terminates when the return statement is encountered in the main() function. To represent a normal exit, we return 0 in the main() function.

When the compiler encounters the return statement in the main() function, all the static objects are destroyed freeing up resources. This happens in the reverse order of the initialization of the objects.

For example,

Output:

Hi

The above code does not print hello since the program terminates on encountering the return statement.

Using the try and catch block

There is also another way to use the return statement. We know about the try and catch block in C++. If we think that a code block can throw some exception, we place it in the try block and if the exception occurs it gets caught in the catch block. This prevents the program from terminating in case an exception occurs in C++.

If we use this in the main() function and place the return statement in the catch block, then the program will close normally and the required cleanup of resources will also take place.

See the following example.

Output:

Hi

Using the exit() function

The exit() function is present in the stdlib.h header file and exits the program when encountered. We have to specify the exit code within the function. This code can be constants EXIT_SUCCESS (which is 0) and EXIT_FAILURE which are also specified in the same header file.

It ignores all the statements after the function. It also performs the same cleanup as done in the previous method.

Output:

Hi

In C++, we can use the atexit() function to call some handlers when a program terminates normally. This function is called while using the exit() function.

Using the _Exit() function

This function terminates a program normally without performing any cleanup of resources. It also does not call the atexit() handler during termination.

We need to specify the exit code in this function also.

For example,

Output:

Hi

Using the quick_exit() function

The quick_exit() function exits a program normally without performing complete cleanup. It calls the specifiers of at_quick_exit() instead of calling the at_exit() during termination.

After calling the at_quick_exit() function, it calls the _Exit() function to terminate the program.

See the code below.

Output:

Hi

Using the abort() function

This function is defined in the cstdlib.h header file and causes an abnormal exit from the program and should be used in extreme cases only. No cleanup is done and only a signal is sent to the OS about the termination.

It does not call the at_exit() or at_quick_exit() functions during the termination.

Output:

Hi

Using the terminate() function

This function is defined in the exception header file. The C++ runtime directly calls this function when the program cannot execute further due to reasons like an exception being thrown and not caught and more.

When called directly, it calls the terminate_handler() function and then executes the abort() function discussed previously to cause an abnormal termination.

For example,

Output:

Hi
terminate called without an active exception

Conclusion

In this article, we discussed how to close a program in C++. Several methods were discussed. Normally, a program terminates in C++ when the return statement is encountered in the main() function. This also cleans up the allocated resources to static objects. We also discussed how to use this in a catch block to terminate a program in C++. Another good way to exit a program in C++ is by using the exit() function. The remaining methods of the quick_exit(), _Exit(), abort(), and terminate() do not perform the proper cleanup to free up the utilized resources.

That’s all about how to exit program in C++.

Was this post helpful?

Leave a Reply

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