C语言 错误:“for”循环初始声明仅在 C99 模式下允许
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29338206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: ‘for’ loop initial declarations are only allowed in C99 mode
提问by Rajit s rajan
I am getting the below error, what is std=c99/std=gnu99 mode?
我收到以下错误,什么是 std=c99/std=gnu99 模式?
source Code:
源代码:
#include <stdio.h>
void funct(int[5]);
int main()
{
int Arr[5]={1,2,3,4,5};
funct(Arr);
for(int j=0;j<5;j++)
printf("%d",Arr[j]);
}
void funct(int p[5]) {
int i,j;
for(i=6,j=0;i<11;i++,j++)
p[j]=i;
}
Error Message:
hello.c: In function ‘main':
hello.c:11:2: error: ‘for' loop initial declarations are only allowed in C99 mode
for(int j=0;j<5;j++)
^
hello.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code`
回答by Alejandro Díaz
This happens because declaring variables inside a for loop wasn't valid C until C99(which is the standard of C published in 1999), you can either declare your counter outside the for as pointed out by others or use the -std=c99 flag to tell the compiler explicitly that you're using this standard and it should interpret it as such.
发生这种情况是因为在 for 循环中声明变量在 C99 之前不是有效的 C(这是 1999 年发布的 C 标准),您可以在其他人指出的 for 之外声明您的计数器或使用 -std=c99 标志明确地告诉编译器你正在使用这个标准,它应该这样解释。
回答by MySequel
You need to declare the variable j used for the first for loop before the loop.
您需要在循环之前声明用于第一个 for 循环的变量 j。
int j;
for(j=0;j<5;j++)
printf("%d",Arr[j]);
回答by Kashif
Easiest Solution by "Prof. Dr. Michael Helbig" . it will switch your mode to c99 so you don't have to add flag every time in make file http://www.bigdev.de/2014/10/eclipse-cc-for-loop-initial.html?showComment=1447925473870#c6845437481920903532
“迈克尔·赫尔比格教授”的最简单解决方案。它会将您的模式切换为 c99,因此您不必每次都在 make 文件中添加标志 http://www.bigdev.de/2014/10/eclipse-cc-for-loop-initial.html?showComment=1447925473870 #c6845437481920903532
Solution: use the option -std=c99 for your compiler! Go to: Project > Properties > C/C++ Buils > Settings > Tool Settings > GCC C Compiler > Dialect > Language Standard: choose "ISO C99"
解决方案:为您的编译器使用选项 -std=c99!转到:项目 > 属性 > C/C++ 构建 > 设置 > 工具设置 > GCC C 编译器 > 方言 > 语言标准:选择“ISO C99”
回答by Yasir Majeed
This will be working code
这将是工作代码
#include <stdio.h>
void funct(int[5]);
int main()
{
int Arr[5]={1,2,3,4,5};
int j = 0;
funct(Arr);
for(j=0;j<5;j++)
printf("%d",Arr[j]);
}
void funct(int p[5]){
int i,j;
for(i=6,j=0;i<11;i++,j++)
p[j]=i;
}

