我安装了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所示,
错误:仅在C99模式下允许'for'循环初始声明注意:使用选项-std = c99或-std = gnu99编译代码
我认为,这消除了您的疑问。i
在函数的开头声明。正确的代码在WorkingExample中将是这样的。
从C99 Wiki开始,这是一个较早版本不可用的新功能。
混合的声明和代码:变量声明不再受限于文件范围或复合语句(块)的开始,类似于C ++
关于VC ++编译器,直接从Herb Stutter先生的口中得知,
完全实现标准C90(使用/ TC或将文件命名为something.c)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句