tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

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

Thu Sep 28, 2017 9:12 am

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?

User avatar
PeterO
Posts: 5075
Joined: Sun Jul 22, 2012 4:14 pm

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

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

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

Thu Sep 28, 2017 9:28 am

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!

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

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

Thu Sep 28, 2017 10:17 am

By the way, in Stretch (the recent version of Raspbian) it now defaults to C11 which is the latest C standard.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

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

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23900
Joined: Sat Jul 30, 2011 7:41 pm

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

Thu Sep 28, 2017 11:02 am

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++)
{
}
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

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.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23900
Joined: Sat Jul 30, 2011 7:41 pm

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

Thu Sep 28, 2017 11:28 am

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.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Thu Sep 28, 2017 11:50 am

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

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

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).

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.
If this post appears in the wrong forums category, my apologies.

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

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

Fri Sep 29, 2017 6:27 am

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.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Fri Sep 29, 2017 7:37 am

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.

User avatar
Paeryn
Posts: 2704
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

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

Fri Sep 29, 2017 11:47 am

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.
She who travels light — forgot something.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

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++)

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23900
Joined: Sat Jul 30, 2011 7:41 pm

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

Fri Sep 29, 2017 2:06 pm

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.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Fri Sep 29, 2017 3:49 pm

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.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

Fri Sep 29, 2017 4:02 pm

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
$
If this post appears in the wrong forums category, my apologies.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Fri Sep 29, 2017 4:10 pm

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.
Last edited by Stiller on Fri Sep 29, 2017 4:16 pm, edited 1 time in total.

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

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

Fri Sep 29, 2017 4:12 pm

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.

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

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

Fri Sep 29, 2017 4:13 pm

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?

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

Fri Sep 29, 2017 4:15 pm

and in the documentation:
*WHICH* documentation? Which OS/version-of-gcc did you get that from?
If this post appears in the wrong forums category, my apologies.

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

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

Fri Sep 29, 2017 4:16 pm

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)
Last edited by jahboater on Fri Sep 29, 2017 4:18 pm, edited 1 time in total.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Fri Sep 29, 2017 4:18 pm

I am compiling with g++ , not gcc
can perhaps that be the reason why?

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

Fri Sep 29, 2017 4:19 pm

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.
If this post appears in the wrong forums category, my apologies.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

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

Fri Sep 29, 2017 4:22 pm

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:
Last edited by Stiller on Fri Sep 29, 2017 4:23 pm, edited 1 time in total.

Return to “C/C++”