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`
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 .
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.
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.
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With