c - 错误: 'for' loop initial declarations are only allowed in c99 mode

标签 c gcc compiler-errors c89

我遇到了这个问题,我只能使用 gcc -std=c99 进行编译,但是,我需要它使用 c89 又名 gcc -Wall 进行编译。这是我使用“for”循环的代码的一部分。请看看您是否可以帮助我,提前谢谢您。

#include<stdio.h>
int main()
{
    int arr[100],i=0,ch;
    int n = 1, sum = 0;
    printf("Check out our selection! \n");
    printf("Airhead - 25 cents\n");
    printf("Fun Dip - 40 cents\n");
    printf("Gummi Bears - 20 cents\n");
    while (n != 0)
    {
        printf("Insert Coins: ");
        scanf("%d",&n);
        arr[i++] = n;
    }

    for(int j=0;j<i;j++)
    {   sum = sum + arr[j];
    }
......

最佳答案

这是错误的:

for (int j = 0; j < i; j++) {
    sum = sum + arr[j];
}

您必须在函数开头初始化j

 int main() {
    int j;
    ...
    for (j = 0; j < i; j++) {
        sum = sum + arr[j];
    }
}

关于c - 错误: 'for' loop initial declarations are only allowed in c99 mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44221148/

相关文章:

C - 控制到达非空函数的结尾

c - 将值推送到结构时堆栈崩溃

c++ - GCC/G++ 编译器设置 GUI

c - 出现编译错误

java - XUGGLE 错误 : Exception in thread "main" java. lang.NoClassDefFoundError: org/slf4j/LoggerFactory

c++ - 必须在赋值中赋值

c - 将 printf 重定向到管道 C

java - Jikes rvm运行时错误 "Could not find the class java.util.Scanner"

c++ - 无法将 'std::basic_istream<char>' 左值绑定(bind)到 'std::basic_istream<char>&&'

c++ - 返回值两份