Friday, November 5, 2010

C & C : Error: 'for' loop initial declaration used outside c99 mode

C & C : Error: 'for' loop initial declaration used outside c99 mode

Source code:
--------------------------------------------
for(int i=0; i<10; i++)
     printf("%d\n", a[i]);
--------------------------------------------
Reason:
The for loop declares the loop variable as part of the for loop itself. This feature was added to the language with the C99 standard; it's not supported in C90.

You can either use C99 mode (add -std=c99 option, but beware: gcc doesn't fully support C99; see <>), or you can re-write the code to be compatible with C90:
--------------------------------------------
int i;
for(i=0; i<10; i++)
     printf("%d\n", a[i]);
--------------------------------------------

No comments:

Post a Comment