22

I got the following error message while compiling the C code:

error: 'for' loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code

What does it mean?

How to fix it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mpluse
  • 1,857
  • 6
  • 18
  • 25

4 Answers4

35

You have done this:

for (int i=0;i<10;i++) {

And you need to change it to this:

int i;
for (i=0;i<10;i++) {

Or, as the error says,

use option -std=c99 or -std=gnu99 to compile your code.

Update copied from Ryan Fox's answer:

gcc -std=c99 foo.c -o foo

Or, if you're using a standard makefile, add it to the CFLAGS variable.

dcorking
  • 1,146
  • 1
  • 14
  • 24
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
7

You'll still need C99 if you want to mix statements and variable declarations. As other answers and the error message itself say, add -std=c99 to the command-line when you compile to enable C99 features [1].

But you have always been allowed to write a compound statement (a "block", IOW, but the standard never uses this word!) in place of a single statement.

#include<stdio.h>
int main() {
    int i = 5;

    {   /* new block, new declarations. */
        int i;
        for (i=0;i<10;i++){
        }
    }
    printf("%d\n", i);  /* prints "5\n" */
}

This is legal in K&R, C90 (aka C89, it's the same thing), and C99.

Enabling C99 mode gets you lots of cool stuff, but it also disables some other cool stuff that gcc allows by default, like anonymous structures and unions within structures and unions.

  1. -std=gnu99 probably enables "all the goodies", but I caution you to avoid doing this. It will make unnecessary difficulty if you (or others) wish to port the code. I'd probably have a windows version of my pet project, ported for free by somebody, had I not done this very thing. It ties you gcc. You don't want to be tied. That's the whole bloody point of standards.
Community
  • 1
  • 1
luser droog
  • 18,988
  • 3
  • 53
  • 105
  • In your post you mention `C90`. What's the difference between `C89` and `C90`? For example; `Section 3.3.3` of the `Introduction to GCC` book gives `-std=c89`, `-std=iso9899` and `-std=c99`. I didn't see a `C90'. – Andy J Dec 15 '14 at 04:55
  • 1
    The ANSI committee adopted the first C standard in 1989. The ISO committee adopted the same standard in 1990. It's the same thing. – luser droog Dec 15 '14 at 05:00
  • 1
    Yes. The original ANSI committee worked very hard to make the standard ready for the international community. There were just a few changes which the ISO committee made, but then the ANSI committee adopted the changes. So they're exactly the same. So, yes, they can be used interchangeably (but be prepared to explain that they're the same if somebody is confused! :) . – luser droog Dec 15 '14 at 05:17
6

The other answers give you a work around to deal with GCC's default mode. If you'd like to use C99, (which I do recommend in general) then you have to add that compiler flag:

gcc -std=c99 foo.c -o foo

Or, if you're using a standard makefile, add it to the CFLAGS variable.

Ryan Fox
  • 10,103
  • 5
  • 38
  • 48
2

It means you can't declare variables in for statement.

You should do:

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

What you are probably doing

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