38

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`
Mathemats
  • 1,185
  • 4
  • 22
  • 35
Rajit s rajan
  • 575
  • 3
  • 7
  • 16
  • 2
    You need either to declare j outside of the for loop, or compile using the -std=c99 option like the error message states. – Loocid Mar 30 '15 at 04:03

4 Answers4

73

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.

user438383
  • 5,716
  • 8
  • 28
  • 43
Alex Díaz
  • 2,303
  • 15
  • 24
16

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]);
MySequel
  • 171
  • 3
1

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 http://www.bigdev.de/2014/10/eclipse-cc-for-loop-initial.html?showComment=1447925473870#c6845437481920903532

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"

Kashif
  • 19
  • 1
-4

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;
    }
Yasir Majeed
  • 739
  • 3
  • 12