exit, _Exit, _exitexit, _Exit, _exit

终止调用进程。Terminates the calling process. 退出函数将终止该进程清理; 后 _exit_Exit立即终止。The exit function terminates it after cleanup; _exit and _Exit terminate it immediately.

备注

不要使用此方法关闭通用 Windows 平台 (UWP) 应用,除测试或调试方案中。Do not use this method to shut down a Universal Windows Platform (UWP) app, except in testing or debugging scenarios. 编程或 UI 方式关闭应用商店应用程序不允许根据Microsoft 存储策略Programmatic or UI ways to close a Store app are not permitted according to the Microsoft Store policies. 有关详细信息,请参阅UWP 应用生命周期For more information, see UWP App lifecycle. 有关 Windows 10 应用的详细信息,请参阅 Windows 10 应用的操作方法指南For more information about Windows 10 apps, see How-to guides for Windows 10 apps.

语法Syntax

void exit(
   int const status
);
void _Exit(
   int const status
);
void _exit(
   int const status
);

参数Parameters

状态退出状态代码。status Exit status code.

备注Remarks

退出_Exit_exit函数会终止调用进程。The exit, _Exit and _exit functions terminate the calling process. 退出函数调用析构函数的线程本地对象,然后调用-按后进先出 (LIFO) 顺序-通过注册的函数atexit_onexit,终止进程之前刷新所有文件缓冲区。The exit function calls destructors for thread-local objects, then calls—in last-in-first-out (LIFO) order—the functions that are registered by atexit and _onexit, and then flushes all file buffers before it terminates the process. _Exit_exit函数会终止进程,而无需销毁线程本地对象或处理atexit_onexit函数,且无需刷新流缓冲区。The _Exit and _exit functions terminate the process without destroying thread-local objects or processing atexit or _onexit functions, and without flushing stream buffers.

尽管退出_Exit_exit调用不返回一个值中的值状态提供给主机环境或者,如果存在,在进程退出后等待调用进程。Although the exit, _Exit and _exit calls do not return a value, the value in status is made available to the host environment or waiting calling process, if one exists, after the process exits. 通常情况下,调用方集状态值为 0 来指示正常退出,或为其他值以指示错误。Typically, the caller sets the status value to 0 to indicate a normal exit, or to some other value to indicate an error. 状态值是可用于操作系统批处理命令ERRORLEVEL和由两个常量之一表示: EXIT_SUCCESS,后者表示值为 0,或EXIT_FAILURE,它表示的值为 1。The status value is available to the operating-system batch command ERRORLEVEL and is represented by one of two constants: EXIT_SUCCESS, which represents a value of 0, or EXIT_FAILURE, which represents a value of 1.

退出_Exit_exitquick_exit_cexit,和 _c_exit函数的行为,如下所示。The exit, _Exit, _exit, quick_exit, _cexit, and _c_exit functions behave as follows.

函数Function 描述Description
exitexit 执行完整的 C 库终止过程,终止进程,并向主机环境提供提供的状态代码。Performs complete C library termination procedures, terminates the process, and provides the supplied status code to the host environment.
_Exit_Exit 执行最少的 C 库终止过程,终止进程,并向主机环境提供提供的状态代码。Performs minimal C library termination procedures, terminates the process, and provides the supplied status code to the host environment.
_exit_exit 执行最少的 C 库终止过程,终止进程,并向主机环境提供提供的状态代码。Performs minimal C library termination procedures, terminates the process, and provides the supplied status code to the host environment.
quick_exitquick_exit 执行快速的 C 库终止过程,终止进程,并向主机环境提供提供的状态代码。Performs quick C library termination procedures, terminates the process, and provides the supplied status code to the host environment.
_cexit_cexit 执行完整的 C 库终止过程并返回给调用方。Performs complete C library termination procedures and returns to the caller. 不终止进程。Does not terminate the process.
_c_exit_c_exit 执行最少的 C 库终止过程并返回给调用方。Performs minimal C library termination procedures and returns to the caller. 不终止进程。Does not terminate the process.

当调用退出_Exit_exit函数,在调用时存在的任何临时或自动对象的析构函数不调用的。When you call the exit, _Exit or _exit function, the destructors for any temporary or automatic objects that exist at the time of the call are not called. 自动对象是在函数中定义的非静态本地对象。An automatic object is a non-static local object defined in a function. 临时对象是由编译器,如函数调用所返回的值创建的对象。A temporary object is an object that's created by the compiler, such as a value returned by a function call. 若要销毁自动对象,然后才能调用退出_Exit,或 _exit,请显式调用析构函数的对象,如下所示:To destroy an automatic object before you call exit, _Exit, or _exit, explicitly call the destructor for the object, as shown here:

void last_fn() {}
    struct SomeClass {} myInstance{};
    // ...
    myInstance.~SomeClass(); // explicit destructor call
    exit(0);
}

不要使用DLL_PROCESS_ATTACH调用退出DllMainDo not use DLL_PROCESS_ATTACH to call exit from DllMain. 若要退出DLLMain函数中,返回FALSEDLL_PROCESS_ATTACHTo exit the DLLMain function, return FALSE from DLL_PROCESS_ATTACH.

要求Requirements

函数Function 必需的标头Required header
退出_Exit_exitexit, _Exit, _exit <process.h> 或 <stdlib.h><process.h> or <stdlib.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_exit.c
// This program returns an exit code of 1. The
// error code could be tested in a batch file.

#include <stdlib.h>

int main( void )
{
   exit( 1 );
}

请参阅See also

进程和环境控制Process and Environment Control
abortabort
atexitatexit
_cexit、_c_exit_cexit, _c_exit
_exec、_wexec 函数_exec, _wexec Functions
_onexit、_onexit_m_onexit, _onexit_m
quick_exitquick_exit
_spawn、_wspawn 函数_spawn, _wspawn Functions
system、_wsystemsystem, _wsystem