Error: ‘For’ Loop Initial Declarations Are Only Allowed in C99 Mode [Duplicate]

I am getting the below error, what is std=c99/std=gnu99 mode?

source Code:

#include <stdio.h>

void funct(int[5]);

int main() 
{        
    int Arr[5]={1,2,3,4,5};
    funct(Arr);
    for(int j=0;j<5;j++)
    printf("%d",Arr[j]);
}

void funct(int p[5]) {
        int i,j;
        for(i=6,j=0;i<11;i++,j++)
            p[j]=i;
}


Error Message:
hello.c: In function ‘main’:
hello.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<5;j++)
      ^
hello.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code`
1

This happens because declaring variables inside a for loop wasn't valid C until C99(which is the standard of C published in 1999), you can either declare your counter outside the for as pointed out by others or use the -std=c99 flag to tell the compiler explicitly that you're using this standard and it should interpret it as such.

2

You need to declare the variable j used for the first for loop before the loop.

    int j;
    for(j=0;j<5;j++)
    printf("%d",Arr[j]);

Easiest Solution by "Prof. Dr. Michael Helbig" . it will switch your mode to c99 so you don't have to add flag every time in make file

Solution: use the option -std=c99 for your compiler! Go to: Project > Properties > C/C++ Buils > Settings > Tool Settings > GCC C Compiler > Dialect > Language Standard: choose "ISO C99"

This will be working code

#include <stdio.h>

    void funct(int[5]);
    int main()
    {
         int Arr[5]={1,2,3,4,5};
         int j = 0;

        funct(Arr);

        for(j=0;j<5;j++)
        printf("%d",Arr[j]);
    }
    void funct(int p[5]){
        int i,j;
        for(i=6,j=0;i<11;i++,j++)
            p[j]=i;
    }
1
Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest