473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C99 and for loops

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
9 19325

"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
* 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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5102
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
15
6555
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
2117
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
46
9868
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
6
1700
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
17
3031
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
3948
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
2
2284
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are...
3
2393
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
8
7234
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.