• Register
1 vote
177 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.
8 5 2
3,230 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.

5 4 2
17,150 points
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

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 176 views
176 views
Problem : Looking for solution of following doubt if one for loop is nested within another, then the counter variables for the two loops should be
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 44 views
44 views
Problem: I just started Bachelors in Software Engineering. Programming is the main subject of Software Engineering and I have no prior knowledge of programming. My programming teacher gave me an assignment in which I have to write a program in C++ whose output should be as ... a hint: Use nested loop. If someone knows how to do this kindly explain and provide me with the example program. Thanks.
asked Nov 29, 2020 Code Learner 6.3k points
0 votes
1 answer 6 views
6 views
Problem: Hello! I have got an exercise in which I have to arrange four integers in ascending order, and I can use only loops and conditional statements. I do not know how I can do this as I do not have a lot of hands-on experience. If someone could provide me with the sample program, I would be very thankful.
asked Nov 30, 2020 Code Learner 6.3k points
0 votes
1 answer 2 views
2 views
Problem: I am reading the book Beginning Programming with Java for Dummies 2nd. Edition, and there is a discussion about using a loop to create multiple objects. Can someone please help fix this, I am new in python(tag name) : How creating multiple objects in a loop java
asked 3 days ago Ali 5.7k points
0 votes
1 answer 2 views
2 views
Problem: What I'd like to know is if the cost of repeatedly calling the charAt method during a long iteration ends up being either less than or greater than the cost of performing a single call to toCharArray at the beginning and then directly accessing the array ... in mind JIT warm-up time, JVM start-up time, etc. and not just the difference between two calls to System.currentTimeMillis().
asked 6 days ago Ali 5.7k points
0 votes
1 answer 29 views
29 views
Problem: I am confused about the do-while loop. My question is simple. In a do-while loop, which block executes first? Why? Does the while always execute?
asked Jan 17 Code Learner 6.3k points
0 votes
1 answer 24 views
24 views
Problem: My programming teacher gave us an assignment in which we have to tell which of the three loops in C++ does not have an entry condition? As far as my guess is concerned, the 'for' loop does not need an entry condition. I want you to guide me if I am wrong and tell me the correct answer to this question. Thanks!
asked Dec 17, 2020 Code Learner 6.3k points
0 votes
1 answer 12 views
12 views
In the first loop that runs with ++ of the function it returns 29 and the second that runs with + i returns 30, but both should return 29. What am I doing? #include <iostream> using namespace std; int sumElements (int a [], int size) {      int accum = 0;      int * p;      p = & a [0]; ...      } int main () {      int array [5] = {4,9,6,7,3};      cout << sumElements (array, 5);      return 0; }
asked Aug 14, 2020 game 4.6k points
1 vote
2 answers 236 views
236 views
Problem : I am very new to programming world. According to my senior a for loop will always get executed at least once even if a required condition is not met. Please find below example for your reference: for (int i=6; i <=5; i++) {     //inappropriate for this ... } According to my senior above listed loop will be executed at least once but it does not or let me know if I am I missing something?
asked Apr 6, 2020 morrisBson 3.2k points
0 votes
1 answer 32 views
32 views
Problem: Hello Programmers, I am a noob and just came to know about the loop constructs in C#. I tried running while and do-while loops in a program and got different output. Following are the programs that I executed: //Program 1 int a = 0; do { Console.WriteLine( ... ; } Console.ReadKey(); The output has confused me and I want to know the major difference in the while and do-while loops. Thanks!
asked Nov 28, 2020 Code Learner 6.3k points