Error in variable declaration in loop


I made the following command:

for(int i = 1 ; i <= 3 ; i++) {etc

So I gave the following error when I compiled:

game.c:11:2: error: "for" loop initial declarations are only allowed in C99 mode
for(int i = 1 ; i <= 3 ; i++) {
/\ //essa seta apontando pro "f"
game.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code

What to do?

asked by anonymous 18.09.2015 / 01:14

3 answers


In the version of C you are using it is not allowed to create a variable within a command for

Try to separate the instructions in:

int i;
for (i = 1; i <= 3; i++){ etc... }
18.09.2015 / 01:19

Do what the message is saying, put -std=c99 on the command line when compiling.

If you can not interpret a simple error message that says what you should do, you will have great difficulty scheduling, this happens all the time. Especially in C you have hairy bugs. So pay attention to what the compiler tells you, it may not be able to solve for you, but it says important things. In simple cosias so just follow what it says in the error message.

Or you can do what the other answers say but this is an archaic technique that should be avoided if there is no reason to use it.

18.09.2015 / 01:17

Place the declaration of the integer outside the loop :

int i;
for(i = 1 ; i <= 3 ; i++) { ... }

Possibly this version of the compiler is quite old, or for the C89 version .

Another solution you can try is to compile as follows:

$ gcc -g game.c -std=c99


$ gcc -g game.c -std=gnu99
18.09.2015 / 01:17