Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]

Tags:

c

loops

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` 
like image 612
Rajit s rajan Avatar asked Mar 30 '15 04:03

Rajit s rajan


People also ask

How do you solve the error for loops initial declarations are only allowed in C99 or C11 mode?

solution. Declaring variables in a for loop is only allowed in C99 or C11 mode, and you need to select C99 as the language standard under Tools/complier option/code generation .

What is C11 mode in C?

C11 is the informal name for ISO/IEC 9899:2011, the current standard for the C language that was ratified by ISO in December 2011. C11 standardizes many features that have already been available in common contemporary implementations, and defines a memory model that better suits multithreading.


2 Answers

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.

like image 112
Alejandro Díaz Avatar answered Sep 18 '22 03:09

Alejandro Díaz


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]); 
like image 38
MySequel Avatar answered Sep 17 '22 03:09

MySequel