【问题标题】:why the following cannot compile under Visual Studio 2010 C++?为什么以下无法在 Visual Studio 2010 C++ 下编译?
【发布时间】:2012-06-18 22:48:50
【问题描述】:
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
  if (argc < 2)
  {
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
  }

  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

我收到以下错误

错误 1 ​​错误 C2143:语法错误:缺少 ';'在“类型”之前
错误 2 错误 C2143:语法错误:缺少 ';'在“类型”之前出现错误 3 错误 C2065:“输入值”:未声明的标识符
错误 4 错误 C2065: 'outputValue' : 未声明的标识符

【问题讨论】:

  • 为我编译。我认为代码不是一对一的翻译。
  • 顺便说一句,如果在调用atof 之后inputValue0,那么用户是否输入了0 或无效的内容? (提示:你不知道)
  • Visual C++ 会根据扩展名(.c 或 .cpp)自动选择 C ​​或 C++。如果将其编译为 C,那么这里有许多副本可以回答您的问题:stackoverflow.com/questions/8496853/…
  • @Mysticial Sounds 发现了我

标签: c++ visual-studio-2010 visual-c++


【解决方案1】:

如果您将文件命名为 .cpp,它应该可以正常编译和运行。

但是,如果您将文件命名为 .c,它将失败。

原因是需要在 C 函数的顶部声明所有变量;您不能在使用时声明它们。

【讨论】:

  • 是的,但背后的原因是VS只实现了C89。在 C99 中,您可以在函数中任意位置声明变量。该死的你MS没有对我们C狂热分子表示爱......
  • 我不知道哪个版本的 VS 声称支持哪个版本的 C ;) 但很明显,MSVS2010 并不完全支持 C99 ;)
  • 这是一个阴谋,期间。 MS 可能希望 C 像 Linux 一样消亡。
  • @EdS:Herb Sutter 还公开表示,他们将永远不会在 his blog 上支持更新版本。
  • @JesseGood:是的,我看到了。 VS 是 C89,除非 MS 改变主意,否则它将永远存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多