C/C中的exit()与_exit()++

退出() _退出() 在C/C++中,它们的功能非常相似。然而,exit()和_exit()之间有一个区别,那就是 exit()函数在程序终止之前执行一些清理,比如连接终止、缓冲区刷新等。

null

退出()

在C中, 退出() 终止调用进程,而不执行exit()函数后面的rest代码。

语法:

void exit(int exit_code); // The exit_code is the value which is returned to parent process

例子:

C

// C program to illustrate exit() function.
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main( void )
{
printf ( "START" );
exit (0);
// The program is terminated here
// This line is not printed
printf ( "End of program" );
}


输出

START

说明: 在上面的程序中,首先调用printf语句并打印值。之后,调用exit()函数,它立即退出执行,并且不打印printf()中的语句。

_退出()

C/C++中的_Exit()函数在不执行任何清理任务的情况下正常终止程序。例如,它不执行使用atexit注册的函数。

语法:

void _Exit(int exit_code); // Here the exit_code represent the exit
                           // status of the program which can be 
                           // 0 or non-zero.

返回值: 函数的_Exit()不返回任何内容。

CPP

// C++ program to demonstrate use of _Exit()
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main( void )
{
int exit_code = 10;
printf ( "Termination using _Exit" );
_Exit(exit_code);
}


在这里,输出将为零。

让我们通过一个例子来理解这种差异 . 在下面的程序中,我们使用 退出() ,

CPP

// A C++ program to show difference
// between exit() and _Exit()
#include <bits/stdc++.h>
using namespace std;
void fun( void ) { cout << "Exiting" ; }
// Driver Code
int main()
{
atexit (fun);
exit (10);
}


输出

Exiting

遇到exit()后,代码立即终止。现在,如果我们用 _退出() ,

CPP

// A C++ program to show difference
// between exit() and _Exit()
#include <bits/stdc++.h>
using namespace std;
void fun( void ) { cout << "Exiting" ; }
int main()
{
atexit (fun);
_Exit(10);
}


没有输出,也没有打印内容。

本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享