By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

C99 and for loops

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Lars Tackmann" <ro****@diku.dk> wrote in message
news:Pi*******************************@brok.diku.d k...
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.
Nov 14 '05 #2

P: n/a
* Thus spoke Lars Tackmann <ro****@diku.dk>:

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
Nov 14 '05 #3

P: n/a
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" <ro****@diku.dk> wrote in message
news:Pi*******************************@brok.diku.d k...
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.

Nov 14 '05 #4

P: n/a
Lars Tackmann <ro****@diku.dk> wrote in
news:Pi*******************************@brok.diku.d k:
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 ->
--
Nov 14 '05 #5

P: n/a
"Mark A. Odell" wrote:

Lars Tackmann <ro****@diku.dk> wrote in
news:Pi*******************************@brok.diku.d k:
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
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 14 '05 #6

P: n/a
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

Nov 14 '05 #7

P: n/a
Andrey Tarasevich <an**************@hotmail.com> 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) ks***@mib.org <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)
Nov 14 '05 #8

P: n/a
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

Nov 14 '05 #9

P: n/a
Andrey Tarasevich <an**************@hotmail.com> 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) ks***@mib.org <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)
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.