如何修复“在 C99 模式之外使用的 for 循环初始声明"?海湾合作委员会错误?

问题描述:

我正在尝试解决3n+1 问题,我有看起来像这样的 for 循环:

I'm trying to solve the 3n+1 problem and I have a for loop that looks like this:

for(int i = low; i <= high; ++i)
        {
                res = runalg(i);
                if (res > highestres)
                {
                        highestres = res;
                }

        }

不幸的是,当我尝试使用 GCC 进行编译时出现此错误:

Unfortunately I'm getting this error when I try to compile with GCC:

3np1.c:15: 错误:'for' 循环初始在 C99 模式之外使用的声明

3np1.c:15: error: 'for' loop initial declaration used outside C99 mode

我不知道什么是 C99 模式.有什么想法吗?

I don't know what C99 mode is. Any ideas?

我会尝试在循环之外声明 i

I'd try to declare i outside of the loop!

祝你成功解决 3n+1 :-)

Good luck on solving 3n+1 :-)

这是一个例子:

#include <stdio.h>

int main() {

   int i;

   /* for loop execution */
   for (i = 10; i < 20; i++) {
       printf("i: %d
", i);
   }   

   return 0;
}

此处阅读有关 C 中 for 循环的更多信息.

Read more on for loops in C here.