One way is to signal an interrupt:
#include <csignal>
// Generate an interrupt
std::raise(SIGINT);
In C:
#include <signal.h>
raise(SIGINT);
UPDATE: Microsoft Docs says that Windows doesn't really support SIGINT
, so if portability is a concern, you're probably better off using SIGABRT
.
SIGINT is not supported for any Win32 application. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application, such as one in UNIX, to become multithreaded and cause unexpected behavior.
int main
rather thanvoid main
.return 0
is not necessary, though, and is just noise!main
is special in all versions of C++ and also in C since C99. Reaching the final}
ofmain
without returning is equivalent toreturn 0;
so the compiler should not warn, and it cannot corrupt anything. In C89 it's undefined, but that's the exception to the rule, not the general case as you imply by "100% necessary". C++98 and C99 are not new, time to update your knowledge ;-)