有什么方法可以从 C++ 中的主函数返回字符串吗?我在下面提到示例程序
string main(int argv, char* argc[])
{
.
.
return "sucess";
}
最佳答案
标准说:
3.6.1 Main function [basic.start.main]
2/ An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both
- a function of () returning int and
- a function of (int, pointer to pointer to char) returning int
as the type of main. [...]
所以不,你必须返回一个整数。
从您对帖子的评论来看,您似乎只想在标准输出上输出一个字符串,然后您可以:
#include <iostream>
int main()
{
std::cout << "Success" << std::endl;
// No need for return 0; it is implicit in C++
}
正如 Kerrek SB 在评论中所说,return 0;
在 C++ 中是隐含的(标准 § 3.6.1/5):
[...] If control reaches the end of
main
without encountering areturn
statement, the effect is that of executingreturn 0;
关于c++ - 从 C++ 中的主函数返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19219363/