gpt4 book ai didi

c++ - 从 C++ 中的主函数返回字符串

转载 作者:太空狗 更新时间:2023-10-29 19:39:25 24 4
gpt4 key购买 nike

有什么方法可以从 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 a return statement, the effect is that of executing

return 0;

关于c++ - 从 C++ 中的主函数返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19219363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com