When running a program in the console you can close it with ctrl-C, but what we want to discuss here is programmatically exiting. Let's start with the most drastic one, and see why you're better off not using it. The process core module provides a handy method that allows you to programmatically exit from a Node.js program: process.exit. Oct 02, 2017 C Beginner's Tutorial: Pausing a console program the right way. How to stop the output console from closing after running your C program.
The exit function, declared in, 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. Dec 26, 2017 Visual Studio 2017 Professional 15.5.7 and 15.6.7 Offline Installation Profiler for C is missing 0 Solution C intellisense compiler does not treat uuidof as constexpr 1 Solution VC: Deleted overriding function which has looser exception specification causes compilation failure. 15.8.1- 'Automatically close the console when debugging stops' visual studio 2017 version 15.8 windows 10.0 debugger bluesmania reported Aug 20, 2018 at 06:07 PM.
-->
In C++, you can exit a program in these ways:
exit function
The exit function, declared in <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. You can use the constants EXIT_FAILURE and EXIT_SUCCESS, also 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 (global object destructors will be called), whereas abort terminates the program immediately. The abort function bypasses the normal destruction process for initialized global static objects. It also bypasses any special processing that was specified using the atexit function.
atexit function
Use the atexit function to specify actions that execute prior to program termination. No global static objects initialized prior to the call to atexit are destroyed prior to execution of the exit-processing function.
return statement in main
Issuing a return statement from
main is functionally equivalent to calling the exit function. Consider the following example:
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 .
Destruction of static objects
When you call
exit or execute a return statement from main , static objects are destroyed in the reverse order of their initialization (after the call to atexit if one exists). The following example shows how such initialization and cleanup works.
Stop Program Town Of Hempstead![]() Stop Program CaliforniaExampleDev How To Stop Program Console From Closing Center![]()
In the following example, the static objects
sd1 and sd2 are created and initialized before entry to main . After this program terminates using the return statement, first sd2 is destroyed and then sd1 . The destructor for the ShowData class closes the files associated with these static objects.
Another way to write this code is to declare the
ShowData objects with block scope, allowing them to be destroyed when they go out of scope:
See alsoComments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |