How to compile a C project in C99 mode?

c c99
mpluse picture mpluse · Apr 8, 2013 · Viewed 55.5k times · Source

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?

Answer

Blorgbeard is out picture Blorgbeard is out · Apr 8, 2013

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.