C++ cstdlib exit() Function
Example
Stop the program, so the last line is never printed:
cout << "Hello World!\n";
exit(0);
cout << "This line is never printed";
Definition and Usage
The exit() function stops the program right away. The code after exit() does not run.
The number you give to exit() is the exit status, which tells the operating system how the program ended. 0 means that the program ended normally. Other numbers, like 1, usually mean that something went wrong.
The exit function is defined in the <cstdlib> header file.
Tip: Inside main(), return 0; also ends the program. Use exit() when you need to stop the program from another function.
Syntax
void exit(int status);
Parameter Values
| Parameter | Description |
|---|---|
| status | Required. The exit status. Use 0 or EXIT_SUCCESS if the program ended normally, and EXIT_FAILURE if something went wrong. |
Technical Details
| Returns: | Nothing. The function does not return, because the program stops. |
|---|