PDA

View Full Version : [C] Weird loop-problem



heamaster
March 14th, 2009, 11:10 PM
Hi! Maybe I am blind but why does



int main()
{

for( int i = 0; i < 4; i++)
{
}
}


give me

"for-loop initial declaration used outside C99 mode"

dwhitney67
March 14th, 2009, 11:12 PM
Because you are compiling with the C89 standards, which by default is offered by GCC. Specify -std=gnu99 with your GCC options and all should be fine.

heamaster
March 14th, 2009, 11:15 PM
Ok thanks! ;)
BTW, didn't for loops exist in C89?

dwhitney67
March 14th, 2009, 11:16 PM
For-loops existed in C89; what wasn't allowed was the local declaration of the variable 'i'. That's what the error was attempting to tell you... yes, I know, it's a lame error message.

P.S. If you want to remain with C89 standards:


int i = 0;

for (; i < 4; i++)
{
}

hanniph
March 14th, 2009, 11:18 PM
It did, but definitions of new variables inside for loop were not allowed I guess

monkeyking
March 14th, 2009, 11:18 PM
The problem isn't the for loop itself,
but the fact that you declare the counter in the loop itself



int i;
for(i=0;i<len;i++)

jimi_hendrix
March 14th, 2009, 11:18 PM
For-loops existed in C89; what wasn't allowed was the local declaration of the variable 'i'. That's what the error was attempting to tell you... yes, I know, it's a lame error message.

P.S. If you want to remain with C89 standards:


int i = 0;

for (; i < 4; i++)
{
}


ya its a lame one...but why is the default the older C?