C99 and for loops

Discussion in 'C Programming' started by Lars Tackmann, Dec 22, 2003.

  1. I am used to write loops like:

    "for (int i = 0; i < 10; i++);"

    why are these loops not allowed in C99 (i get - `for' loop initial
    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
    syntactly wrong.

    my other compiler (bordland) - do not complain about this, this is the
    first time i have ever seen this.

    thanks.
     
    Lars Tackmann, Dec 22, 2003
    #1
    1. Advertising

  2. * Thus spoke Lars Tackmann <>:

    Hallo,

    > "for (int i = 0; i < 10; i++);"
    >
    > why are these loops not allowed in C99 (i get - `for' loop initial
    > declaration used outside C99 mode - when trying this with gcc 3.2.2),


    They are. If you want to turn the C99 mode on, use:

    $ gcc -std=c99 foobar.c -o foobar


    Wolfgang.
    --
    "Erfahrungen -- das sind die vernarbten Wunden unserer Dummheit."
    -- John Osborne
     
    Wolfgang Kaufmann, Dec 22, 2003
    #2
    1. Advertising

  3. Lars Tackmann

    Tim Prince Guest

    "Lars Tackmann" <> wrote in message
    news:p...
    > I am used to write loops like:
    >
    > "for (int i = 0; i < 10; i++);"
    >
    > why are these loops not allowed in C99 (i get - `for' loop initial
    > declaration used outside C99 mode - when trying this with gcc 3.2.2),
    >

    gcc is telling you that you haven't set C99 mode, e.g. -std=c99.
     
    Tim Prince, Dec 22, 2003
    #3
  4. Thanks - I just found it strange because this has been allowed before c99
    - mabee not in the ansi c standard but all the compilers i have worked
    with has compiled code like this without complainning - it seams that it first
    officialy became a part of the legal syntax in ansi C with c99.

    On Mon, 22 Dec 2003, Tim Prince wrote:

    >
    > "Lars Tackmann" <> wrote in message
    > news:p...
    > > I am used to write loops like:
    > >
    > > "for (int i = 0; i < 10; i++);"
    > >
    > > why are these loops not allowed in C99 (i get - `for' loop initial
    > > declaration used outside C99 mode - when trying this with gcc 3.2.2),
    > >

    > gcc is telling you that you haven't set C99 mode, e.g. -std=c99.
    >
    >
    >
     
    Lars Tackmann, Dec 22, 2003
    #4
  5. Lars Tackmann <> wrote in
    news:p:

    > Thanks - I just found it strange because this has been allowed before
    > c99
    > - mabee not in the ansi c standard but all the compilers i have worked
    > with has compiled code like this without complainning - it seams that it
    > first officialy became a part of the legal syntax in ansi C with c99.


    Possible you were using a C++ compiler?


    --
    - Mark ->
    --
     
    Mark A. Odell, Dec 22, 2003
    #5
  6. "Mark A. Odell" wrote:
    >
    > Lars Tackmann <> wrote in
    > news:p:
    >
    > > Thanks - I just found it strange because this has been allowed before
    > > c99
    > > - mabee not in the ansi c standard but all the compilers i have worked
    > > with has compiled code like this without complainning - it seams that it
    > > first officialy became a part of the legal syntax in ansi C with c99.

    >
    > Possible you were using a C++ compiler?
    >
    > --
    > - Mark ->
    > --


    Quite probably. He mentioned Borland, right? Mine is C++ , not C.

    --
    Julian V. Noble
    Professor Emeritus of Physics

    ^^^^^^^^^^^^^^^^^^
    http://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Dec 22, 2003
    #6
  7. Lars Tackmann wrote:
    > Thanks - I just found it strange because this has been allowed before c99
    > - mabee not in the ansi c standard but all the compilers i have worked
    > with has compiled code like this without complainning - it seams that it first
    > officialy became a part of the legal syntax in ansi C with c99.
    > ....


    It is highly unlikely that you could compile this code with any pre-C99
    C compiler. Most likely you were using C++ compilers. There's nothing
    strange in the fact that C++ allows this and C89/C90 doesn't.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 23, 2003
    #7
  8. Andrey Tarasevich <> writes:
    > Lars Tackmann wrote:
    > > Thanks - I just found it strange because this has been allowed
    > > before c99 - mabee not in the ansi c standard but all the
    > > compilers i have worked with has compiled code like this without
    > > complainning - it seams that it first officialy became a part of
    > > the legal syntax in ansi C with c99.
    > > ....

    >
    > It is highly unlikely that you could compile this code with any pre-C99
    > C compiler. Most likely you were using C++ compilers. There's nothing
    > strange in the fact that C++ allows this and C89/C90 doesn't.


    I think pre-C99 versions of gcc provide this as a language extension.
    Other compilers may provide similar extensions.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
     
    Keith Thompson, Dec 23, 2003
    #8
  9. Keith Thompson wrote:
    >> > Thanks - I just found it strange because this has been allowed
    >> > before c99 - mabee not in the ansi c standard but all the
    >> > compilers i have worked with has compiled code like this without
    >> > complainning - it seams that it first officialy became a part of
    >> > the legal syntax in ansi C with c99.
    >> > ....

    >>
    >> It is highly unlikely that you could compile this code with any pre-C99
    >> C compiler. Most likely you were using C++ compilers. There's nothing
    >> strange in the fact that C++ allows this and C89/C90 doesn't.

    >
    > I think pre-C99 versions of gcc provide this as a language extension.
    > Other compilers may provide similar extensions.
    > ...


    Yes, but if I'm not mistaken, these extensions needed to be explicitly
    enabled before they could be used, which normally makes the user to
    understand that he is using an extension, not a standard feature.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 23, 2003
    #9
  10. Andrey Tarasevich <> writes:
    > Keith Thompson wrote:
    > >> > Thanks - I just found it strange because this has been allowed
    > >> > before c99 - mabee not in the ansi c standard but all the
    > >> > compilers i have worked with has compiled code like this without
    > >> > complainning - it seams that it first officialy became a part of
    > >> > the legal syntax in ansi C with c99.
    > >> > ....
    > >>
    > >> It is highly unlikely that you could compile this code with any pre-C99
    > >> C compiler. Most likely you were using C++ compilers. There's nothing
    > >> strange in the fact that C++ allows this and C89/C90 doesn't.

    > >
    > > I think pre-C99 versions of gcc provide this as a language extension.
    > > Other compilers may provide similar extensions.
    > > ...

    >
    > Yes, but if I'm not mistaken, these extensions needed to be explicitly
    > enabled before they could be used, which normally makes the user to
    > understand that he is using an extension, not a standard feature.


    It depends on the compiler. gcc, by default, enables a number of
    language extensions; you have to give it extra command-line options to
    disable the extensions and turn it into a (more or less) conforming
    ISO C compiler. I've seen similar command-line options in other
    compilers.

    In this particular case, though, you appear to be correct; gcc rejects
    "for (int i = 0; i < 10; i ++)" unless you specify C99 mode.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
     
    Keith Thompson, Dec 23, 2003
    #10
    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. Replies:
    13
    Views:
    6,488
    Dave Thompson
    Dec 20, 2004
  2. Replies:
    3
    Views:
    345
    Mark McIntyre
    Aug 16, 2003
  3. Replies:
    3
    Views:
    3,734
    Chris Torek
    Feb 20, 2006
  4. Replies:
    3
    Views:
    621
    Keith Thompson
    Mar 31, 2007
  5. Me
    Replies:
    2
    Views:
    260
Loading...

Share This Page