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