How do I fix "for loop initial declaration used outside C99 mode" GCC error?

The.Anti.9 picture The.Anti.9 · Aug 24, 2008 · Viewed 334.4k times · Source

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;
                }

        }

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

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

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

Answer

OysterD picture OysterD · Aug 24, 2008

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

Good luck on solving 3n+1 :-)

Here's an example:

#include <stdio.h>

int main() {

   int i;

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

   return 0;
}

Read more on for loops in C here.