错误:'for'循环初始声明仅允许在C99模式下使用

Raj*_*jan 19 c loops

我收到以下错误,什么是std = c99/std = gnu99模式?

源代码:

#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`
Run Code Online (Sandbox Code Playgroud)

Ale*_*íaz 35

发生这种情况是因为在for循环中声明变量无效C直到C99(这是1999年发布的C的标准),你可以在其他人指出的for之外声明你的计数器或使用-std = c99标志明确地告诉编译器你正在使用这个标准,它应该解释它.

  • @Rajitsrajan只需将`-std = c99`添加到命令行即`gcc main.c -o main -std = c99` (3认同)
  • 非常感谢亚历杭德罗.请你告诉我如何在linux编译中使用-std = c99标志? (2认同)

小智 9

您需要在循环之前声明用于第一个 for 循环的变量 j。

    int j;
    for(j=0;j<5;j++)
    printf("%d",Arr[j]);
Run Code Online (Sandbox Code Playgroud)