‘for’ loop initial declaration used outside C99 mode

  • Post author:
  • Post category:其他



You can get the error

  'for' loop initial declaration used outside C99 mode

if you try to declare a variable inside a for loop without turning on

C99

mode.

Back in the old days, when dinosaurs roamed the earth and programmers used punch cards, you were not allowed to declare variables anywhere except at the very beginning of a block.


ERROR IN NON-C99 MODE

 for (int i = 0; i<10; i++) 
 {
   printf("i is %d\n", i);
 }


CORRECT

 int i;
 for (i = 0; i<10; i++) 
 {
   printf("i is %d\n", i);
 }


ANOTHER WAY

You can also compile with the C99 switch set. Put -std=c99 in the compilation line:

 gcc -std=c99 foo.c -o foo