Page 1 of 2

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 9:12 am
by tchichua25
Hey everyone,
I'm trying to compile really simple code on my RPi3 using Geany. The code was originally written using code::blocks on windows and it was working without any problem. The errors I'm getting are:
untitled.c:33:5: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
and
untitled.c:33:5: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
As I understand the code is not the problem but the way I'm compiling it. Can you give me any advice?

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 9:22 am
by PeterO
You need to add one of the suggested options to the compile command configured in geany,

In the menu.. Build -> Set Build Commands add "-std=c99" after "gcc" in Compile and Build boxes.

(I'm looking on a MINT PC but I expect the version on PI is the same).
PeterO

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 9:28 am
by tchichua25
PeterO wrote:
Thu Sep 28, 2017 9:22 am
You need to add one of the suggested options to the compile command configured in geany,

In the menu.. Build -> Set Build Commands add "-std=c99" after "gcc" in Compile and Build boxes.

(I'm looking on a MINT PC but I expect the version on PI is the same).
PeterO
Compilation finished successfully.
Yeeyy! Thank you so much, works like a charm!

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 10:17 am
by jahboater
By the way, in Stretch (the recent version of Raspbian) it now defaults to C11 which is the latest C standard.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 11:00 am
by Stiller
tbh I never heard about a for-loop which just compiles (and complies) with C99 or C11, so I'm really curious about the current source code and the actual Geany compile/build-settings

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 11:02 am
by jamesh
Stiller wrote:
Thu Sep 28, 2017 11:00 am
tbh I never heard about a for-loop which just compiles (and complies) with C99 or C11, so I'm really curious about the current source code and the actual Geany compile/build-settings

Code: Select all

/* Works for any C */
int i
for (i=0;i<10;i++)
{
}

Code: Select all

// Requries C99 (although some older compilers do support it)
for (int i=0;i<10;i++)
{
}

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 11:09 am
by Stiller
for (i=0;i<10;i++)
for (int i=0;i<10;i++)
so far I've been using both versions with the standard Geany settings in gcc 5.x and 7.x (Jessie admittedly), regardless of explicitely std=-C99/11 in the Geany settings or not.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 11:28 am
by jamesh
Stiller wrote:
Thu Sep 28, 2017 11:09 am
for (i=0;i<10;i++)
for (int i=0;i<10;i++)
so far I've been using both versions with the standard Geany settings in gcc 5.x and 7.x (Jessie admittedly), regardless of explicitely std=-C99/11 in the Geany settings or not.
I think those versions of the compiler default to at least C99 so will work with both version out of the box.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 11:50 am
by Stiller
exactly! So that's why I can't explain that the OP's Geany or compiler didn't comply with these defaults!
... so I'm really curious about the current source code and the actual Geany compile/build-settings

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Thu Sep 28, 2017 12:04 pm
by Martin Frezman
It depends on which version of gcc you are using. They changed the default standard somewhere along the way (I think [but don't quote me] that up through 5.x, the default was gnu89, as of 6.0, they changed it to gnu99).

This in turn depends on which OS (we tend to assume Raspbian, but it needn't be true) and which version of that OS any one particular poster is running.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 6:27 am
by jahboater
Martin Frezman wrote:
Thu Sep 28, 2017 12:04 pm
It depends on which version of gcc you are using. They changed the default standard somewhere along the way (I think [but don't quote me] that up through 5.x, the default was gnu89, as of 6.0, they changed it to gnu99).
Yes thats right. In fact they changed it all the way to gnu11 and gnu++14).
The compiler that comes with stretch is 6.3 and is C11 by default and C++14.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 7:37 am
by Stiller
I've been using both versions with the standard Geany settings in gcc 5.x and 7.x (Jessie admittedly), regardless of explicitely std=-C99/11 in the Geany settings or not.
The compiler that comes with stretch is 6.3 and is C11 by default and C++14.
so if the OP had Stretch or Jessie he probably wouldn't have run into this issue: both apply to C99 or even C11 from the start.
Thats why I'm still curious about the current source code and the actual Geany compile/build-settings - or which OS he is using instead.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 11:47 am
by Paeryn
Stiller wrote:
Fri Sep 29, 2017 7:37 am
so if the OP had Stretch or Jessie he probably wouldn't have run into this issue: both apply to C99 or even C11 from the start.
Thats why I'm still curious about the current source code and the actual Geany compile/build-settings - or which OS he is using instead.
Gcc in Jessie defaults to gnu90 IIRC.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 12:52 pm
by Stiller
how could then both work with jessie, gcc, and Geany, without std=-C99 / -C11 setting?
for (i=0;i<10;i++)
for (int i=0;i<10;i++)

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 2:06 pm
by jamesh
Stiller wrote:
Fri Sep 29, 2017 12:52 pm
how could then both work with jessie, gcc, and Geany, without std=-C99 / -C11 setting?
for (i=0;i<10;i++)
for (int i=0;i<10;i++)
it doesn't, judging by the original post.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 3:49 pm
by Stiller
no, it does:
Stiller wrote:
Thu Sep 28, 2017 11:09 am
for (i=0;i<10;i++)
for (int i=0;i<10;i++)
so far I've been using both versions with the standard Geany settings in gcc 5.x and 7.x (Jessie admittedly), regardless of explicitely std=-C99/11 in the Geany settings or not.
Stiller wrote:so if the OP had Stretch or Jessie he probably wouldn't have run into this issue: both apply to C99 or even C11 from the start.
Thats why I'm still curious about the current source code and the actual Geany compile/build-settings - or which OS he is using instead.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:02 pm
by Martin Frezman
no, it does:
Yes, it doesn't. Are we having fun yet?

FWIW, I just tested on an up-to-date Jessie system, running GCC 4.9.2, and it rejects shown below:

$ printf "#include <stdio.h>\nint main(void) { for (int i=1; i<5; i++); }\n" | gcc -xc -
<stdin>: In function 'main':
<stdin>:2:18: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
<stdin>:2:18: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
$

Note, of course, that version of OS really isn't the variable; the variable is version of GCC.

Anyway, it turns out to be rather difficult to answer the simple question: What is the default standard for my version of GCC? That is, there's no good "system" way to do it; you have to check the documentation.

The following comes close (so solving our over-arching problem):

$ gcc -dM -E - < /dev/null | grep STDC
#define __STDC_HOSTED__ 1
#define __STDC_IEC_559__ 1
#define __STDC_ISO_10646__ 201103L
#define __STDC_NO_THREADS__ 1
#define _STDC_PREDEF_H 1
#define __STDC_IEC_559_COMPLEX__ 1
#define __STDC__ 1
$

The variable that should be there, but isn't, is __STDC_VERSION__, as shown in the next example:

$ gcc -std=gnu99 -dM -E - < /dev/null | grep STDC
#define __STDC_HOSTED__ 1
#define __STDC_UTF_16__ 1
#define __STDC_IEC_559__ 1
#define __STDC_ISO_10646__ 201103L
#define __GNUC_STDC_INLINE__ 1
#define __STDC_NO_THREADS__ 1
#define _STDC_PREDEF_H 1
#define __STDC_IEC_559_COMPLEX__ 1
#define __STDC_VERSION__ 199901L
#define __STDC_UTF_32__ 1
#define __STDC__ 1
$

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:10 pm
by Stiller
that is really funny - except apt-get update and upgrade I never made changes to my Jessie, nor did I upgrade gcc (except once for a try on a sepaerate SD, but reformatted then everything) - nonetheless both for-loops work without setting a std=-Cxx flag.
gcc is 4.9 .
But I agree, it's a mysterium.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:12 pm
by jahboater
and in the documentation:

Code: Select all

gnu11
gnu1x
   GNU dialect of ISO C11.  This is the default for C code.  The name gnu1x is
   deprecated.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:13 pm
by jahboater
Stiller wrote:
Fri Sep 29, 2017 4:10 pm
that is really funny - except apt-get update and upgrade I never made changes to my Jessie, nonetheless both for-loops work without setting a std=-Cxx flag.
gcc is 4.9 .
But I agree, it's a mysterium.
Unless there is some kind of default setting in your IDE?

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:15 pm
by Martin Frezman
and in the documentation:
*WHICH* documentation? Which OS/version-of-gcc did you get that from?

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:16 pm
by jahboater
Martin Frezman wrote:
Fri Sep 29, 2017 4:15 pm
and in the documentation:
*WHICH* documentation? Which OS/version-of-gcc did you get that from?
Sorry, "man gcc"

Raspbian stretch / the default compiler (gcc 6.3)

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:18 pm
by Stiller
I am compiling with g++ , not gcc
can perhaps that be the reason why?

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:19 pm
by Martin Frezman
Stiller wrote:
Fri Sep 29, 2017 4:18 pm
I am compiling with g++ , not gcc
can perhaps that be the reason why?
Well, yes. As in "NOW you tell us???"

This feature has been in C++ since forever. It was gradually back-fitted into C.

Re: Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Posted: Fri Sep 29, 2017 4:22 pm
by Stiller
well, ok, I'm a beginner to C, I didn't know there is a difference between gcc and g++ compiler calls in this respect :mrgreen: