Home > database >  Set CodeBlocks for ISO C99 mode only
Set CodeBlocks for ISO C99 mode only

Time:03-06

How can I set my compiler to allow compiling only the code which was written in ISO C99 mode?

I have done the following:

Project -> Properties -> Project's build options -> Here I selected ISO C99

However, when I try to compile:

#include <stdio.h>
int main()
{
    for(int i=0;i<5;i  )
        printf("%d",i);
    return 0;
}

I don't get any warnings, I should get warning:

for loop declarations are not allowed in C99 mode.

Could you help me fix this?

CodePudding user response:

C99 does support declarations of variable inside the iteration statement of a for-loop. See 6.8.5 of the C99 standard for reference:

iteration-statement:

while ( expression ) statement

do statement while ( expression ) ;

for ( expressionopt ; expressionopt ; expressionopt ) statement

for ( declaration expression opt ; expressionopt ) statement

6.8.5.3 further clarifies the scoping of variables declared in a for-loop iteration clause:

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.

CodePudding user response:

GNU GCC uses the following to check for syntax conforming to C99 standards

-std=c99 -pedantic

which issue all the warnings demanded by strict ISO C99 standard.

However, https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Warning-Options.html mentions that:

Some users try to use -pedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all — only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.

A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -pedantic. We don't have plans to support such a feature in the near future.

Let's say your program is named main.c (with the example code given by you) you can use the following command from a shell terminal

gcc -o main -std=c99 -pedantic main.c

CodePudding user response:

Now I get warning

'for' loop initial declarations are only allowed in C99 or C11 mode

The problem was that this was not allowed in C90 mode, and it is allowed in C99 mode.

  • Related