在C++ stdlib中使用哪个函数退出程序执行状态代码?
在Java中,有:
System.exit(0)
Run Code Online (Sandbox Code Playgroud)
Mat*_*haq 12
假设你只有一个线程:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return(0);
// PROGRAM ENDS HERE.
std::cout << "You should not see this.\n";
return(0);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello, World!
Run Code Online (Sandbox Code Playgroud)
它return(0);可以放在你喜欢的任何地方 - 它会结束int main(),因此你的程序.
或者,您可以打电话exit(EXIT_SUCCESS);或exit(EXIT_FAILURE);从任何地方打电话:
/* exit example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
pFile = fopen("myfile.txt", "r");
if(pFile == NULL)
{
printf("Error opening file");
exit (1);
}
else
{
/* file operations here */
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)