Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to enable for(int i=0; ... in gcc without having to turn on c99 mode

I've got a very big program which compiles with gcc without warnings.

If I turn on c99 mode --std=c99 on the command line, it gives a huge number of warnings and errors.

But I love the idiom for(int i=0; i<20; i++){ code }

in place of {int i; for (i=0; i<20; i++){ code }}

Is there any way to tell gcc to allow this and only this?

Alternatively, is there any way to enable c99 mode in the particular functions I'm working on? Something like

#pragma c99 on 

for(int i=0; i<99; i++)
{
    code
}

#pragma c99 off
like image 892
John Lawrence Aspden Avatar asked Feb 07 '12 11:02

John Lawrence Aspden


People also ask

How do I fix error for loop initial declarations are only allowed in c99 mode?

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

What is c99 mode in C?

C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard.

How do I enable c99 mode in code blocks?

very simple. In the project properties (right click over project->properties) click "Project's build options..." button, then in "Compiler settings" tab, click in "Other options" sub-tab. Type "-std=c99" in text area and thats all. cheers!!.


2 Answers

It is likely that the warnings and errors are because -std=c99 requests standard-conforming C99, which means that many platform-specific functions that pollute the C99 namespace are not defined.

Instead, you should try --std=gnu99, which is the C99-equivalent to the default mode of gnu89.

like image 96
caf Avatar answered Oct 26 '22 18:10

caf


Alternatively to use -std=gnu99 you can disable individual warnings:

-Wno-declaration-after-statement

Read info gcc:

`-Wdeclaration-after-statement (C and Objective-C only)'
     Warn when a declaration is found after a statement in a block.
     This construct, known from C++, was introduced with ISO C99 and is
     by default allowed in GCC.  It is not supported by ISO C90 and was
     not supported by GCC versions before GCC 3.0.  *Note Mixed
     Declarations::.
like image 43
gavenkoa Avatar answered Oct 26 '22 19:10

gavenkoa