Reputation: 44668
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?
Upvotes: 126
Views: 392862
Reputation: 2076
Enable C99 mode in Code::Blocks 16.01
Upvotes: 2
Reputation: 41
Jihene Stambouli answered OP question most directly... Question was; why does
for(int i = low; i <= high; ++i)
{
res = runalg(i);
if (res > highestres)
{
highestres = res;
}
}
produce the error;
3np1.c:15: error: 'for' loop initial declaration used outside C99 mode
for which the answer is
for(int i = low...
should be
int i;
for (i=low...
Upvotes: 2
Reputation: 897
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):
export CFLAGS="-std=c99"
Note that a similar solution applies if you run into a similar scenario with C++ compilation that requires C++ 11, you can use:
export CXXFLAGS="-std=c++11"
Upvotes: 6
Reputation: 6750
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.
Upvotes: 160
Reputation: 4003
To switch to C99 mode in CodeBlocks, follow the next steps:
Click Project/Build options, then in tab Compiler Settings choose subtab Other options, and place -std=c99
in the text area, and click Ok.
This will turn C99 mode on for your Compiler.
I hope this will help someone!
Upvotes: 23
Reputation: 15011
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.
Upvotes: 125
Reputation: 103467
I've gotten this error too.
for (int i=0;i<10;i++) { ..
is not valid in the C89/C90 standard. As OysterD says, you need to do:
int i;
for (i=0;i<10;i++) { ..
Your original code is allowed in C99 and later standards of the C language.
Upvotes: 14
Reputation: 19
I had the same problem and it works you just have to declare the i
outside of the loop:
int i;
for(i = low; i <= high; ++i)
{
res = runalg(i);
if (res > highestres)
{
highestres = res;
}
}
Upvotes: 1
Reputation: 21
For Qt-creator: just add next lines to *.pro file...
QMAKE_CFLAGS_DEBUG = \
-std=gnu99
QMAKE_CFLAGS_RELEASE = \
-std=gnu99
Upvotes: 0
Reputation: 71
if you compile in C change
for (int i=0;i<10;i++) { ..
to
int i;
for (i=0;i<10;i++) { ..
You can also compile with the C99 switch set. Put -std=c99 in the compilation line:
gcc -std=c99 foo.c -o foo
REF: http://cplusplus.syntaxerrors.info/index.php?title='for'_loop_initial_declaration_used_outside_C99_mode
Upvotes: 6
Reputation: 91019
New Features in C99
http://en.wikipedia.org/wiki/C99
Upvotes: 13