C99 and for loops

L

Lars Tackmann

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.
 
W

Wolfgang Kaufmann

* Thus spoke Lars Tackmann <[email protected]>:

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.
 
T

Tim Prince

Lars Tackmann said:
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.
 
L

Lars Tackmann

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.
 
M

Mark A. Odell

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?
 
J

Julian V. Noble

Mark A. Odell said:
Possible you were using a C++ compiler?

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

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
A

Andrey Tarasevich

Lars said:
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.
 
K

Keith Thompson

Andrey Tarasevich said:
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.
 
A

Andrey Tarasevich

Keith said:
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.
 
K

Keith Thompson

Andrey Tarasevich said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top