exit c+_C / C ++中的exit()与_Exit()和示例

exit c+

exit()函数 (exit() function)

exit() function is used to terminate the program normally with several cleanups like buffers flushing, terminating the connections, etc before exiting from the program.

exit()函数通常用于在退出程序之前进行多次清理,例如清除缓冲区缓冲区,终止连接等,以终止程序。

Syntax:

句法:

void exit(int status);

_Exit()函数 (_Exit() function)

_Exit() function is also used to terminate the program normally without cleanups like buffers flushing, terminating the connections, etc before exiting from the program.

_Exit()函数也可用于正常退出程序,而无需退出程序前进行清理,如缓冲区刷新,终止连接等。

It was introduced in C11.

它是在C11中引入的。

Syntax:

句法:

void _Exit(int status);

Parameter(s): status – defines the exit status.

参数: status –定义退出状态。

There are two status that we can define,

我们可以定义两种状态,

ValueMacroDescription
0EXIT_SUCCESSIt defines that the termination is on the success of the program.
1EXIT_FAILUREIt defines that the termination is on the failure of the program.
巨集 描述
0 EXIT_SUCCESS 它定义了程序成功的终止。
1个 EXIT_FAILURE 它定义终止是在程序失败时进行的。

Return value: The return type of the function is void, it returns nothing.

返回值:函数的返回类型为void ,不返回任何内容。

Note: In both functions, if the value of status is 0 or EXIT_SUCCESS, an implementation-defined status indicating successful termination is returned to the host environment. If the value of status is EXIT_FAILURE, an implementation-defined status, indicating unsuccessful termination, is returned. In other cases, an implementation-defined status value is returned.

注意:在这两个函数中,如果status的值为0或EXIT_SUCCESS ,则表示成功终止的实现定义状态将返回到主机环境。 如果status的值为EXIT_FAILURE ,则返回实现定义的状态,指示终止失败。 在其他情况下,将返回实现定义的状态值。

exit()函数示例 (Example of exit() function)

// C++ program to demonstrate the
// example of exit()

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello, world...");

    exit(0);

    printf("Bye, bye!!!...");

    return 0;
}

Output:

输出:

Hello, world...

_Exit()函数的示例 (Example of _Exit() function)

// C++ program to demonstrate the
// example of _Exit()

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello, world...");

    _Exit(0);

    printf("Bye, bye!!!...");

    return 0;
}

Output:

输出:



Explanation:

说明:

See both of the examples, in Example 1, there is a printf() function before the exit(0) which will terminate the program. As I told, exit() function cleanups the several things thus it will also flush the output stream and "Hello, world..." will be printed. In Example 2, _Exit(0) will terminate the program and will not clean up the things thus it will also not flush the output stream and nothing will be printed.

参见两个示例,在示例1中 ,在exit(0)之前有一个printf()函数,它将终止程序。 就像我说过的那样, exit()函数清理了几件事,因此它还将刷新输出流,并且将打印“ Hello,world ...” 。 在示例2中_Exit(0)将终止程序,并且不会清除内容,因此也将不刷新输出流,并且不会打印任何内容。

Reference: _Exit() function

参考: _Exit()函数

翻译自: https://www.includehelp.com/cpp-tutorial/exit-vs-_exit-in-c-cpp-with-examples.aspx

exit c+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值