for loops in C/djgpp C99 mode?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
OSNewbie

for loops in C/djgpp C99 mode?

Post by OSNewbie »

i have been doing some testing of my console display functions and i wrote something like the following:

for (int i=0, i<100, i++)
{
console_putDec(i);
}

when i compiled using djgpp, it said something like "initial for loop declaration outside of C99 mode" what is C99 mode and why cant i do such a for loop?
ka3r

RE:for loops in C/djgpp C99 mode?

Post by ka3r »

I don't know what is C99 mode and I do not use DJGPP, but you "for" statement is wrong: The initialization part, the condition part and the increment part are separated by ';', not by commas ',': try this out:

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

It should work!
common

RE:for loops in C/djgpp C99 mode?

Post by common »

C99 is the newest C standard which does allow for declarations to be outside of blocks (if applied to a for loop, for example).  Allowing flexibility of this nature, similiar to C++ in fashion.  C89/C90 did not allow for this.  C99 has not been vastly implemented on many systems, it's best to simply put for in a new block and use it that way and conform to C89/C90.  However, if you absolutely *MUST* use C99, you may need to send your compiler a flag to do so...gcc would need -std=c99, for example.

{
int i;
for(i = 0; i < 100 ; i++)
{
  console_putDec(i);
}
}
OSNewbie

RE:for loops in C/djgpp C99 mode? thank you

Post by OSNewbie »

thanks, putting the loop inside of a block with curly braces worked great!
Post Reply