我在大学安装了 VS Express 2012 来学习 C 语言。我创建新的空项目,然后将新项目添加到 Source Files 文件夹并将 Source.cpp 更改为 Source.c
“Hello World”运行没有问题,但是当我像这样编写简单的“for”循环时:
#include <stdio.h>
#include <stdlib.h>
int main() {
for (int i = 0; i <= 6; i++)
{
printf("the i value is: %d\n", i);
}
getchar();
return (0);
}
它给我写了很多错误:
------ Build started: Project: cTest, Configuration: Debug Win32 ------
Source.c
e:\ctest\source.c(7): error C2143: syntax error : missing ';' before 'type'
e:\ctest\source.c(7): error C2143: syntax error : missing ')' before 'type'
e:\ctest\source.c(7): error C2065: 'i' : undeclared identifier
e:\ctest\source.c(7): warning C4552: '<=' : operator has no effect; expected operator with side-effect
e:\ctest\source.c(7): error C2059: syntax error : ')'
e:\ctest\source.c(8): error C2143: syntax error : missing ';' before '{'
e:\ctest\source.c(9): error C2065: 'i' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
请帮我解决这个问题。
最佳答案
显示于 IdeOne ,
error: ‘for’ loop initial declarations are only allowed in C99 mode note: use option -std=c99 or -std=gnu99 to compile your code
我想,这消除了你的疑虑。在函数的开头声明i
。
正确的代码如下 WorkingExample .
来自C99 Wiki ,这是一项之前未提供的新功能。
Intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), similar to C++
关于VC++编译器,直接来自Mr. Herb Stutter's mouth ,
Implements Standard C90 exactly (using /TC or naming files as something.c)
关于c - 在 Visual Studio Express 2012 中运行 C 代码的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23406479/