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?

3
  • 1
    Well, maybe showing you code would be a good start. Apr 8, 2013 at 2:57
  • 1
    Pull the initialization out of the for-loop.
    – Mysticial
    Apr 8, 2013 at 2:57
  • 13
    The compiler says how to fix it in the error message.
    – chris
    Apr 8, 2013 at 2:58

4 Answers 4

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.

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.
3
  • 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, 2014 at 4: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. Dec 15, 2014 at 5: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! :) . Dec 15, 2014 at 5: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.

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++ )
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.