c++ - 为什么在main中可以返回一个const char*?

标签 c++

谁能解释一下为什么这段代码在编译时不会出错?

int main()
{
  // why doesn't the following line give a type mismatch error??/
  return "success!";
}

最佳答案

因为三字母 ??/ 被替换为 \,这意味着返回被注释掉了。你的代码等同于

int main()
{
  // why doesn't the following line give a type mismatch error\
  return "success!";
}

相同
int main()
{
  // why doesn't the following line give a type mismatch error return "success!";
}

还要注意,在 main 中没有 return 语句的情况下,有一个隐式的 return 0;,所以代码是定义明确。

关于c++ - 为什么在main中可以返回一个const char*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22282777/

相关文章:

c++ - 求和 XOR 的直接公式

c++ - 关于 shared_ptr 析构函数中实现错误的困惑

C++ 二进制表达式树 : How do I print an infix expression with appropriate parentheses?

android - 没有任何java的原生android开发?

C++ Tic-Tac-Toe 使用类

c# - 将带有 std::vector 的 C++ 结构映射到 C#

c++ - C/C++ 查找指针

C++ tolower 特殊字符,如 ü

c++ - 词法和语法分析器软件

c++ - 未评估上下文中的 "Cannot call member function ... without object"- GCC 错误?