Re: Error: 'for' loop initial declaration used outside c99 mode



james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.edu> wrote:

Pedro Pinto wrote:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode

What is it and how can i solve it?

You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:

Unless you severely hobble your C, that is not generally possible.

for( int k=0; k<10; k++){}

A better solution is to _read_ the error message. It clearly (and
correctly) states that this construct is not legal ISO C outside C99;
the obvious (and also correct) conclusion is that it _is_ legal ISO C99.
There are two reasonable solutions to this problem:

- stick with C89 (or even pre-ANSI C), and move the declaration outside
the for loop, to the beginning of any available block (probably the
function block);
- read the documentation of your compiler, which evidently does have a
C99 mode, and use that mode.

Richard
.



Relevant Pages

  • Re: Compile Error
    ... that I changed main to int main- your version was wrong. ... test2.c:11: warning: implicit declaration of function `randomize' ... test2.c:56: error: `for' loop initial declaration used outside C99 mode ...
    (comp.lang.c)
  • Re: Error: for loop initial declaration used outside c99 mode
    ... 'for' loop initial declaration used outside c99 mode ... so may be accepted if you are compiling C with a C++ compiler: ... I do like that syntax since it's such a common idiom to have a loop ...
    (comp.lang.c)
  • C99 and for loops
    ... declaration used outside C99 mode - when trying this with gcc 3.2.2), ... are there any bad side efects in this code or do people just find it ...
    (comp.lang.c)
  • Re: Error: for loop initial declaration used outside c99 mode
    ... 'for' loop initial declaration used outside c99 mode ... so may be accepted if you are compiling C with a C++ compiler: ... Unless you severely hobble your C, ...
    (comp.lang.c)
  • Error: for loop initial declaration used outside c99 mode
    ... When compiling my program i got this error: ... 'for' loop initial declaration used outside c99 mode ...
    (comp.lang.c)