• Register
113,234 points
20 14 10

In this post I will show you the solution to the following problem that occurs when programming in C:

Table of contents:

  • The problem.
  • Compiler solution.
  • Solution in code.

file.c: 34: 5: error: 'for' loop initial declarations are only allowed in C99 mode

Personally it happens to me when trying to compile with gcc , but in the same way if it appears elsewhere, I will show you the solution.


The problem:

The problem causing this error is that you are declaring the variable within the for loop ; which (for some strange reason) you shouldn't do. That is, you have something like the following:

for ( int y = 0 ; y <ROWS; y ++) {
    // ...

If you notice, in this case the variable y is being declared within the definition of the loop. That causes the problem.

Compiler solution:

The simplest solution is to compile with the option -std=c99. For example, if in your case you compile like this:

gcc archivo.c

Just pass the indicated option, like this:

gcc archivo.c -std=c99

The error should go away and your code should compile like a charm.

Solution in code:

To avoid the error message : 'for' loop initial declarations are only allowed in C99 mode, the variable must be declared outside the loop. So:

int and;
for (y = 0 ; y <ROWS; y ++) {
    // ...

In this case the variable is declared outside, and within the loop it is assigned a value.

I don't know why this happens; since when programming in CLion everything is fine, but if I try to compile with gcc this happens. There must be a fair explanation, but for now this is the way to fix it.

More Posts

Greatest common divisor in C - Euclid's algorithm amna - Oct 4, 2020
How do we use for-each loop in arrays muliemes - May 18
How to iterate through string in C++? Lalit Kumar - May 8, 2020
Difference between for and while loop Sanjana Sagar - May 29
How to play youtube video in loop amna - 6 days
How to loop through characters in a string java amna - Jul 2
What does break do in c++? Lalit Kumar - Nov 27, 2020
Loop Quiz Questions and Answers You Need to Know Sana Sabir - Jan 27
Error in c++ "a function-definition is not allowed here before '{' token" Lalit Kumar - Jul 11, 2020
How to check if all elements in a array are equal Tania - May 12