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?
>Solution :
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 ) statementbehaves 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.