Tho*_*ews 15 c visual-studio-2010 variable-declaration
我有编译GNUARM编译器的代码,但Visual Studio 2010发出错误.该问题涉及在C语言文件中的第一个语句之后声明变量:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 6;
i = i + 1;
printf("Value of i is: %d\n", i);
int j = i * 10; // <-- This is what Visual Studio 2010 complains about.
printf("Value of j is: %d\n", j);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
以下代码编译时没有错误:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 6;
int j; // <-- Declaration is now here, valid according to K&R rules.
i = i + 1;
printf("Value of i is: %d\n", i);
j = i * 10; // <-- Moved declaration of j to above.
printf("Value of j is: %d\n", j);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用默认设置来创建Win32控制台项目.当我将"编译为"属性设置为"编译为C++(/ TP)"时,我在某些Visual Studio头文件中出现编译错误.(右键单击项目,选择" 属性" →" 配置属性" →" C/C++" →" 高级").
如何告诉Visual Studio 2010在第一个语句之后允许变量声明,如C++或当前的C语言标准?
你没有.Visual C++不支持C99.
您需要编译为C++(并相应地更新您的代码)或遵循C89的规则.
(我不知道你会得到什么错误与编译时/TP
,我可以成功编译你的例子/TP
,如果我加#include <stdlib.h>
了EXIT_SUCCESS
;如果你提供更多的细节,我或其他人可能能够帮助.)