Nov 10, 2011 Dev C and C exit function Forum: Bloodshed Software Forum. Creator: nonlinearly. Why when I use it without include this header the the dev-c does not throw any error? Only stdio.h But according to the ANSI standard library the exit function belongs to the stdlib.h! Try to use 'return' in place of break when you want to run rest of code normally. Just use return. More info can be found here. In C, you can return from a function any time you want. Break is to exit a loop or a switch construct. Instead, use return with an optional value.
< cpp | utility | program
C++Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
Type support (basic types, RTTI, type traits) | |||||||||||||||
Dynamic memory management | |||||||||||||||
Error handling | |||||||||||||||
Program utilities | |||||||||||||||
Variadic functions | |||||||||||||||
Library feature-test macros | |||||||||||||||
Date and time | |||||||||||||||
Function objects | |||||||||||||||
Formatting library(C++20) | |||||||||||||||
(C++11) | |||||||||||||||
(C++20) | |||||||||||||||
(C++11) | |||||||||||||||
(C++14) | |||||||||||||||
Relational operators (deprecated in C++20) | |||||||||||||||
Comparisons (C++20) | |||||||||||||||
Integer comparison functions | |||||||||||||||
(C++20) | |||||||||||||||
Common vocabulary types | |||||||||||||||
| |||||||||||||||
Swap, forward and move | |||||||||||||||
(C++14) | |||||||||||||||
(C++11) | |||||||||||||||
(C++11) | |||||||||||||||
(C++11) | |||||||||||||||
Elementary string conversions | |||||||||||||||
(C++17) | |||||||||||||||
(C++17) | |||||||||||||||
(C++17) | |||||||||||||||
Type operations | |||||||||||||||
(C++11) | |||||||||||||||
(C++17) | |||||||||||||||
(C++17) |
Program termination | |||||
| |||||
Communicating with the environment | |||||
Signals | |||||
Signal types | |||||
Non-local jumps | |||||
Types |
Defined in header <cstdlib> | |
(until C++11) | |
[[noreturn]]void abort()noexcept; | (since C++11) |
Causes abnormal program termination unless SIGABRT is being caught by a signal handler passed to std::signal and the handler does not return.
Destructors of variables with automatic, thread local(since C++11) and static storage durations are not called. Functions registered with std::atexit()and std::at_quick_exit(since C++11) are also not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.
[edit]Parameters
(none)
[edit]Return value
(none)
[edit]Exceptions
(none)
[edit]Notes
POSIX specifies that the abort() function overrides blocking or ignoring the SIGABRT signal.
[edit]Example
Output:
[edit]See also
causes normal program termination with cleaning up (function)[edit] | |
registers a function to be called on std::exit() invocation (function)[edit] | |
(C++11) | causes quick program termination without completely cleaning up (function)[edit] |
(C++11) | registers a function to be called on quick_exit invocation (function)[edit] |
sets a signal handler for particular signal (function)[edit] | |
C documentation for abort |
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/abort&oldid=117409'
-->In C++, you can exit a program in these ways:
- Call the exit function.
- Call the abort function.
- Execute a return statement from
main
.
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.
Dev C++ Programs
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.Example
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.How To Use Exit Function In Dev C++
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: