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

标签 c++

有什么方法可以从 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/

相关文章:

c++ - 不推荐使用 b2Body::SetUserData

数据结构的 C++ 排序算法

C++ 析构函数

c++ - 正确实现纹理图集

c++ - ISampleGrabber 可以将视频帧转换为特定的媒体类型吗?

c++ - 获取 QStringList 的前一个元素

c++ - 将源中的 std::clog 移动到输出文件

c++ - 实现流操作符时编译错误

c++ - valgrind 关于 Jenkins : How to stop it? 启动的无尽程序

c++ - 根据模板参数调用不同版本的模板成员函数