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?
For anyone attempting to compile code from an external source that uses an automated build utility such as Make, to avoid having to track down the explicit gcc compilation calls you can set an environment variable. Enter on command prompt or put in .bashrc (or .bash_profile on Mac):
Note that a similar solution applies if you run into a similar scenario with C++ compilation that requires C++ 11, you can use:
@Blorgbeard:
New Features in C99
http://en.wikipedia.org/wiki/C99
A Tour of C99
if you compile in C change
to
You can also compile with the C99 switch set. Put -std=c99 in the compilation line:
REF: http://cplusplus.syntaxerrors.info/index.php?title='for'_loop_initial_declaration_used_outside_C99_mode
There is a compiler switch which enables C99 mode, which amongst other things allows declaration of a variable inside the for loop. To turn it on use the compiler switch
-std=c99
Or as @OysterD says, declare the variable outside the loop.
I'd try to declare
i
outside of the loop!Good luck on solving 3n+1 :-)
Here's an example:
Read more on for loops in C here.