在C++中,exit(); 是一个用于终止程序的函数调用。它来自C标准库中的 stdlib.h 头文件。
具体来说:
exit()函数会立即终止程序的执行,并返回给操作系统。在程序退出之前,它会执行一些清理操作,如关闭所有打开的文件和释放动态分配的内存
exit() 是一个函数,用于立即终止程序的执行,你可以传递其他整数值来表示不同的退出状态。通常,0 表示成功,而非零值表示某种错误。
当参数是0时,表示程序正常终止,并返回状态码0
当参数是1时,程序会提前退出,并返回状态码1
以下是一个简单的示例:
#include <cstdlib> // for exit()
#include <iostream>
int main() {
std::cout << "Starting the program..." << std::endl;
// Simulate some condition that makes the program exit early.
if (/* some condition */) {
std::cout << "Program is exiting early due to a condition." << std::endl;
exit(0); // Exit the program with status code 0.
}
std::cout << "Continuing the program..." << std::endl;
// ... other code ...
return 0; // Also a way to exit with status code 0.
}
注意:直接使用 exit(0); 会立即终止程序,不会执行 main 函数的返回语句之后的任何代码。在上面的示例中,如果满足某个条件,程序会提前退出,不会打印 “Continuing the program…”。