You can get the error
'for' loop initial declaration used outside C99 mode
if you try to declare a variable inside a for loop without turning on C99 mode.
Back in the old days, when dinosaurs roamed the earth and programmers used punch cards, you were not allowed to declare variables anywhere except at the very beginning of a block.
ERROR IN NON-C99 MODE
for (int i = 0; i<10; i++)
{
printf("i is %d\n", i);
}
CORRECT
int i;
for (i = 0; i<10; i++)
{
printf("i is %d\n", i);
}
ANOTHER WAY You can also compile with the C99 switch set. Put -std=c99 in the compilation line:
gcc -std=c99 foo.c -o foo
本文介绍了一个常见的C语言编译错误——在非C99模式下尝试在for循环内声明变量。文章提供了错误示例及两种解决方法:一是将变量声明移至循环外部;二是使用C99标准进行编译。
1万+

被折叠的 条评论
为什么被折叠?



