473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error: 'for' loop initial declaration used outside c99 mode

When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode
What is it and how can i solve it?

Thanks in advance!

Regards

Nov 3 '06 #1
8 74544
"Pedro Pinto" <ku*****@gmail. comwrites:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode
What is it and how can i solve it?
It's an error message about some code that you failed to show us. In
general, you can't expect us to know what the problem is unless you
show us the actual code as well as the error message.

In this case, you've lucked out. You probably have something like this:

...
for (int i = 0; i < N; i ++) {
...
}
...

which declares the loop variable as part of the for loop itself. This
feature was added to the language with the C99 standard; it's not
supported in C90.

You can either use C99 mode (but beware: gcc doesn't fully support
C99; see <http://gcc.gnu.org/c99status.html> ), or you can re-write
the code to be compatible with C90:

...
int i;
...
for (i = 0; i < N; i ++) {
...
}
...

which is legal C99 as well.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 3 '06 #2
Pedro Pinto wrote:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode
What is it and how can i solve it?
You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:

for( int k=0; k<10; k++){}

I do like that syntax since it's such a common idiom to have a loop
iterator only in the scope of the loop.

Nov 3 '06 #3

james of tucson escreveu:
Pedro Pinto wrote:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode
What is it and how can i solve it?

You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:

for( int k=0; k<10; k++){}

I do like that syntax since it's such a common idiom to have a loop
iterator only in the scope of the loop.

Hi again!

Yes, the issue was that i was declaring the int i inside the for! =)

And yes, i first started programming in Java, thus my dificulties in
understanding certain apects of C! Thanks a million

Nov 3 '06 #4
james of tucson <jmcgill@[go_ahead_and_sp am_me].arizona.eduwro te:
Pedro Pinto wrote:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode

What is it and how can i solve it?

You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:
Unless you severely hobble your C, that is not generally possible.
for( int k=0; k<10; k++){}
A better solution is to _read_ the error message. It clearly (and
correctly) states that this construct is not legal ISO C outside C99;
the obvious (and also correct) conclusion is that it _is_ legal ISO C99.
There are two reasonable solutions to this problem:

- stick with C89 (or even pre-ANSI C), and move the declaration outside
the for loop, to the beginning of any available block (probably the
function block);
- read the documentation of your compiler, which evidently does have a
C99 mode, and use that mode.

Richard
Nov 3 '06 #5
Richard Bos wrote:
james of tucson <jmcgill@[go_ahead_and_sp am_me].arizona.eduwro te:

>>Pedro Pinto wrote:
>>>When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode

What is it and how can i solve it?

You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:


Unless you severely hobble your C, that is not generally possible.
Severely hobble is going a bit far, just avoid the subset of C that
doesn't intersect with C++.
--
Ian Collins.
Nov 3 '06 #6
Ian Collins <ia******@hotma il.comwrote:
Richard Bos wrote:
james of tucson <jmcgill@[go_ahead_and_sp am_me].arizona.eduwro te:
>This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:
Unless you severely hobble your C, that is not generally possible.

Severely hobble is going a bit far, just avoid the subset of C that
doesn't intersect with C++.
That's what I said. No properly written malloc() calls, for starters.

Richard
Nov 3 '06 #7
On Thu, 2006-11-02 at 21:42 -0800, Pedro Pinto wrote:
When compiling my program i got this error:

Error: 'for' loop initial declaration used outside c99 mode
This means that you did

for (int i = 0; i < n; i++)
....

Where you declared the variable i after you executed some
statements. The mixing of declarations and code is illegal
in C90 (the "old" standard), but is legal in C99 (the new
standard).

--
Andrew Poelstra <http://www.wpsoftware. net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.

Nov 3 '06 #8
Richard Bos wrote:
Ian Collins <ia******@hotma il.comwrote:
>>Richard Bos wrote:
>>>james of tucson <jmcgill@[go_ahead_and_sp am_me].arizona.eduwro te:

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:

Unless you severely hobble your C, that is not generally possible.

Severely hobble is going a bit far, just avoid the subset of C that
doesn't intersect with C++.

That's what I said. No properly written malloc() calls, for starters.
Which may be a small price to pay.

Just to put a few things in perspective, let me describe a real life
situation where compiling an embedded C application with a C++ compiler
on the development platform (note: *not* on the target) helped the
development process.

There were three main reasons, firstly I wanted to test device drivers
down to simulated device level. To do this I provided two definitions
of the device registers, in C a struct of bit fields and in C++ a struct
of structs where each bit was a member function, so setting a bit caused
the simulation to react as the real device would.

Second, the test rig had to work with misaligned data (sent to and from
a target system) the host compiler did not support, so again in C the
packets were represented as structs of POD and in C++ as structs of
structs that packed and unpacked the data.

Third, we wanted the extra static type safety offered by C++. We made
heavy use of enums as function parameters and the stricter conversion
rules prevented inappropriate types being passed.

This may be a specialised case, but I have used the same techniques for
many years in the development and testing of embedded systems.

None of this host based testing is a substitute for rigorous acceptance
testing of the target system, built with the target C compiler, it
merely augments it and helps with the development.

--
Ian Collins.
Nov 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6055
by: Christophe Grimault | last post by:
Hi all, look at the following code: ////////////////////////// for(int i = 0; i<10; i++) cout << i; for(int i = 0; i<20; i++) cout << i;
32
3774
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
3
7373
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is declared in the loop header, but this time that isn't the case: I've got a piece of code a bit like this: #include <iostream> #include <cmath>
32
4601
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it...
1
1894
by: sam | last post by:
hi all, i am writing some software to model polymerisation kinetics and have created a way of doing so which involves taking ever smaller slices of time to model until some sort of convergence was observed in the results. so far so good, but i was using 'for i in the range(iterations):' a for loop over each slice of time, where the...
5
3996
by: hprYeV | last post by:
I have done a reasonable amount of programming in C++, but the other day I was talking to someone after a lecture in a course on Java who said that they had not been used to the syntax of the Java for loop because they always had been programming in C++. I asked them what it was they had not been used to, and they said that in C++ you can...
7
18134
by: Ravi | last post by:
I read from the book "The Complete Reference - C" that C99 allows us to declare for loop variables as for(int i=0; i < 10; i++) printf("%d",i); however, my compiler(gcc) gives me the error: " 'for' loop initial declaration used outside C99 mode " Please explain.
5
4037
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
2
3448
by: shalskedar | last post by:
In my database if I try to use the Subform Wizard it gives me the following error: "For Loop not initialised" If I try to use any subform in the Main form, it gives me the same error. Is this error generated if Ms Access database is corrupted?
0
7669
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. ...
0
7926
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...
1
7439
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5987
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...
1
5343
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...
0
4962
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...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
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.