• Register
1 vote
443 views

Problem :

I am very new to Programming. I have recently started learning C Programming. I have written below code but I am facing following error.

Please find below my source code for your reference:

#include <stdio.h>
void myfirstfunct(int[6]);
int main() 
{        
    int Arr[6]={1,2,3,4,5,6};
    myfirstfunct (Arr);
    for(int i=0;i<6;i++)
    printf("%d",Arr[i]);
}
void myfirstfunct(int p[6]) {
        int i,j;
        for(i=7,j=0;i<11;i++,j++)
            p[j]=i;
}
Please find below my error message for your reference:
hello.c: In function ‘main’:
hello.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(inti=0;i<6;i++)
      ^
hello.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code`
I am looking for the very expert advice on quickly fixing above error.
3.2k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

You are very new to C programming so I am trying to help you. You are facing this error because of declaring the variables inside your for loop and it is not valid C programming until the C99. C99 is a standard of C which was published in 1999, you should either declare the counter outside your for loop or you can use a -std=c99 flag to tell your compiler explicitly that now you want to use this standard and so the compiler must interpret it correctly. 

You can also use a option -std=c99 for the compiler. You need to go to below path :

Project > Properties > C/C++ Buils > Settings > Tool Settings > GCC C Compiler > Dialect > Language Standard: Here you need to choose "ISO C99"

Hope you have understood the quick fix.

17.2k points
edited by
0 votes

Solution:

This occurs since declaring variables within 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 employ the -std=c99 flag to inform the compiler apparently that you're employing this standard and it must interpret it as such.

You require to declare the variable j employed for the first for loop before the loop.

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

I'd attempt to declare i outside of the loop!

#include <stdio.h>

int main() {

   int i;

   /* for loop execution */
   for (i = 10; i < 20; i++) {
       printf("i: %d\n", i);
   }   

   return 0;
}

There is a compiler switch which enables C99 mode, which amongst other things approves declaration of a variable within the for loop. To turn it on employ the compiler switch -std=c99

To switch to C99 mode in CodeBlocks, pursue the next steps:

Click Project/Build options, then in tab Compiler Settings choose subtab Other options, and place -std=c99 in the text area, and click Ok.

This will turn C99 mode on for your Compiler.

I've gotten this error too.

for (int i=0;i<10;i++) { ..

is not valid in the C89/C90 standard. As OysterD says, you require to do:

int i;
for (i=0;i<10;i++) { ..

in case you compile in C change

for (int i=0;i<10;i++) { ..

To

int i;
for (i=0;i<10;i++) { ..

You can further compile with the C99 switch set. Put -std=c99 in the compilation line:

gcc -std=c99 foo.c -o foo

 

31.7k points
edited by

Related questions

0 votes
1 answer 10 views
10 views
Problem: Where can I find a solution for this : Error: 'for' loop initial declarations are only allowed in c99 mode?
asked Jun 19 Sawyer Parviz 166k points
0 votes
1 answer 12 views
12 views
Problem: I am getting the below error, hello.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
asked Apr 10 ummesalma 29.1k points
0 votes
1 answer 12 views
12 views
Problem: How do I solve this issue : C for loop initial declarations are only allowed in c99 mode?
asked Jun 22 Shima 122k points
0 votes
1 answer 9 views
9 views
Problem: Need assistance : Loop initial declarations are only allowed in c99 mode
asked Jun 25 nila 117k points
0 votes
1 answer 7 views
7 views
problem: For loop initial declaration are only allowed in c99?
asked Jun 26 Nahil 122k points
1 vote
1 answer 44 views
44 views
Problem: I am getting an error for gcc: Error for loop initial declaration used outside c99 mode.
asked Apr 27 PkGuy 25.9k points
0 votes
1 answer 14 views
14 views
Problem: How can I resolve this error: GCC error: 'for' loop initial declaration used outside C99 mode?
asked Apr 14 Humaira ahmed 50.7k points
0 votes
1 answer 3 views
3 views
Problem: What exactly is the solution : For loop initial declaration are only allowed in c99 mode?
asked Jun 23 Mezbah 125k points
0 votes
1 answer 3 views
3 views
Problem : For loop initial declaration used outside c99 mode.
asked Jun 28 Sifat55 104k points
0 votes
1 answer 15 views
15 views
Problem: Please let me know any solution regarding this .. For loop initial declaration only allowed in c99
asked Apr 7 Ifra 43.4k points