473,657 Members | 2,430 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 19331

"Lars Tackmann" <ro****@diku.dk > wrote in message
news:Pi******** *************** ********@brok.d iku.dk...
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.
--
"Erfahrunge n -- 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.d iku.dk...
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.d iku.dk:
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.d iku.dk:
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*@lessspamfor mother.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_Keit h) 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_Keit h) 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
5110
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 we need to break once more... So maybe there should be an argument to break that is an int which is a number of loops to break. So it would default to 1 (or whatever means the current enclosing loop), not breaking any code...
15
6573
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
2130
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) { /* main bulk of code goes here */ } }
46
9911
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
6
1705
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 < n) { ...
17
3044
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 counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about iterating over an object. The reason for this distinction comes from the fact that I read a lot how...
10
3965
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 than 'while'? C: for(i=0; i<length; i++) python: while i < length:
2
2293
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 there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
3
2410
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 field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
8
7247
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.