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

Discussion in 'C Programming' started by Pedro Pinto, Nov 3, 2006.

  1. Pedro Pinto

    Pedro Pinto Guest

    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?

    Thanks in advance!

    Regards
    Pedro Pinto, Nov 3, 2006
    #1
    1. Advertising

  2. "Pedro Pinto" <> writes:
    > 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?


    It's an error message about some code that you failed to show us. In
    general, you can't expect us to know what the problem is unless you
    show us the actual code as well as the error message.

    In this case, you've lucked out. You probably have something like this:

    ...
    for (int i = 0; i < N; i ++) {
    ...
    }
    ...

    which declares the loop variable as part of the for loop itself. This
    feature was added to the language with the C99 standard; it's not
    supported in C90.

    You can either use C99 mode (but beware: gcc doesn't fully support
    C99; see <http://gcc.gnu.org/c99status.html>), or you can re-write
    the code to be compatible with C90:

    ...
    int i;
    ...
    for (i = 0; i < N; i ++) {
    ...
    }
    ...

    which is legal C99 as well.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 3, 2006
    #2
    1. Advertising

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

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

    I do like that syntax since it's such a common idiom to have a loop
    iterator only in the scope of the loop.
    james of tucson, Nov 3, 2006
    #3
  4. Pedro Pinto

    Pedro Pinto Guest

    james of tucson escreveu:
    > 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:
    >
    > for( int k=0; k<10; k++){}
    >
    > I do like that syntax since it's such a common idiom to have a loop
    > iterator only in the scope of the loop.



    Hi again!

    Yes, the issue was that i was declaring the int i inside the for! =)

    And yes, i first started programming in Java, thus my dificulties in
    understanding certain apects of C! Thanks a million
    Pedro Pinto, Nov 3, 2006
    #4
  5. Pedro Pinto

    Richard Bos Guest

    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
    Richard Bos, Nov 3, 2006
    #5
  6. Pedro Pinto

    Ian Collins Guest

    Richard Bos wrote:
    > 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.
    >

    Severely hobble is going a bit far, just avoid the subset of C that
    doesn't intersect with C++.


    --
    Ian Collins.
    Ian Collins, Nov 3, 2006
    #6
  7. Pedro Pinto

    Richard Bos Guest

    Ian Collins <> wrote:

    > Richard Bos wrote:
    > > james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.edu> wrote:
    > >
    > >>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.

    >
    > Severely hobble is going a bit far, just avoid the subset of C that
    > doesn't intersect with C++.


    That's what I said. No properly written malloc() calls, for starters.

    Richard
    Richard Bos, Nov 3, 2006
    #7
  8. On Thu, 2006-11-02 at 21:42 -0800, Pedro Pinto wrote:
    > When compiling my program i got this error:
    >
    > Error: 'for' loop initial declaration used outside c99 mode


    This means that you did

    for (int i = 0; i < n; i++)
    ....

    Where you declared the variable i after you executed some
    statements. The mixing of declarations and code is illegal
    in C90 (the "old" standard), but is legal in C99 (the new
    standard).

    --
    Andrew Poelstra <http://www.wpsoftware.net>
    For email, use 'apoelstra' at the above site.
    "You're only smart on the outside." -anon.
    Andrew Poelstra, Nov 3, 2006
    #8
  9. Pedro Pinto

    Ian Collins Guest

    Richard Bos wrote:
    > Ian Collins <> wrote:
    >
    >>Richard Bos wrote:
    >>
    >>>james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.edu> wrote:
    >>>
    >>>>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.

    >>
    >>Severely hobble is going a bit far, just avoid the subset of C that
    >>doesn't intersect with C++.

    >
    > That's what I said. No properly written malloc() calls, for starters.
    >

    Which may be a small price to pay.

    Just to put a few things in perspective, let me describe a real life
    situation where compiling an embedded C application with a C++ compiler
    on the development platform (note: *not* on the target) helped the
    development process.

    There were three main reasons, firstly I wanted to test device drivers
    down to simulated device level. To do this I provided two definitions
    of the device registers, in C a struct of bit fields and in C++ a struct
    of structs where each bit was a member function, so setting a bit caused
    the simulation to react as the real device would.

    Second, the test rig had to work with misaligned data (sent to and from
    a target system) the host compiler did not support, so again in C the
    packets were represented as structs of POD and in C++ as structs of
    structs that packed and unpacked the data.

    Third, we wanted the extra static type safety offered by C++. We made
    heavy use of enums as function parameters and the stricter conversion
    rules prevented inappropriate types being passed.

    This may be a specialised case, but I have used the same techniques for
    many years in the development and testing of embedded systems.

    None of this host based testing is a substitute for rigorous acceptance
    testing of the target system, built with the target C compiler, it
    merely augments it and helps with the development.

    --
    Ian Collins.
    Ian Collins, Nov 3, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC
    Replies:
    6
    Views:
    7,227
    Richard Herring
    May 17, 2005
  2. Replies:
    3
    Views:
    3,677
    Chris Torek
    Feb 20, 2006
  3. Gary Wessle
    Replies:
    7
    Views:
    403
  4. Ravi

    C99 declaration of for loop

    Ravi, Aug 25, 2007, in forum: C Programming
    Replies:
    9
    Views:
    755
    Richard
    Aug 26, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    373
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page