C 和 C++ 中的 exit() vs _Exit() 函数
在本节中,我们将了解 C 和 C++ 中 exit() 和 _Exit() 有哪些不同。在 C 中,exit() 会终止调用进程,而不会执行 exit() 函数后的剩余代码。
在 C++11 中,存在一个名为 _Exit() 的新函数。那么这个函数有什么特点呢?exit() 函数在终止程序之前会执行一些清理工作。它会清除连接终止、缓冲区刷新等。而 _Exit() 函数不会清理任何内容。如果我们使用 atexit() 方法测试,它将不起作用。
让我们来看两个示例。在这两个示例中,我们首先使用 exit() 函数,然后在下一个
示例
#include<bits/stdc++.h>
using namespace std;
void my_function(void) {
cout << "Exiting from program";
}
int main() {
atexit(my_function);
exit(10);
}输出
Exiting from program
示例
#include<bits/stdc++.h>
using namespace std;
void my_function(void) {
cout << "Exiting from program";
}
int main() {
atexit(my_function);
_Exit(10);
}输出
In this case the output is blank. Nothing has come.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP