C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Variables in for loop - language change?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
No Spam
Guest





PostPosted: Sun Dec 14, 2003 2:00 am    Post subject: Variables in for loop - language change? Reply with quote



Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Gnu compiler complains about this; Microsoft doesn't.

Under Gnu, must change to

int i;
for (i=0;i {
...
}

The first way of declaring i inside the for statement is mention in
the 3rd edition of "The C++ Programming Language" [Strousptrup]
and "The Annotated C++ Reference Manual" [Ellis and Stroupstrup].



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jack Klein
Guest





PostPosted: Sun Dec 14, 2003 11:06 am    Post subject: Re: Variables in for loop - language change? Reply with quote



On 13 Dec 2003 21:00:14 -0500, "No Spam" <nospam (AT) myisp (DOT) net> wrote in
comp.lang.c++.moderated:

Quote:
Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Gnu compiler complains about this; Microsoft doesn't.

What GNU compiler? There have been many versions. Older ones did not
support this, as it was not part of C++. Newer ones do support it, as
it became part of standard C++.

And are you invoking whatever version of the compiler as a C or a C++
compiler?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Sun Dec 14, 2003 11:07 am    Post subject: Re: Variables in for loop - language change? Reply with quote



No Spam wrote:
Quote:
Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Gnu compiler complains about this; Microsoft doesn't.

No GNU C++ compiler I can remember using has ever
complained about this in C++ code. GCC even gets
the rules right about where the scope of i ends,
and Microsoft have only just changed to getting
that right. (In their defense, compiler vendors
are sometimes trapped between standards compliance
and not breaking their customers' code.)

Quote:
Under Gnu, must change to

int i;
for (i=0;i {
...
}

The first way of declaring i inside the for statement is mention in
the 3rd edition of "The C++ Programming Language" [Strousptrup]
and "The Annotated C++ Reference Manual" [Ellis and Stroupstrup].

Both C and C++ allow the first form now, though C adopted
it only in 1999.

If you do need further information on this, you might try
a few things:
(1) Give the exact error text and a short, close-to-compilable
example which reproduces the error
(2) Say which versions of compilers you are talking about.

The second is important here because GCC's C++ support only
became anywhere near supporting standard C++ with 2.95, and
only a good approximation to a compliant compiler with 3.0
or later.

Is it possible that your code is assuming that the following
is correct?

int main() {
for (int i = 0; i != 10; ++i) {
// empty loop for exposition
}
i = 0;
}

Visual C++ 6 would compile that, though it is not valid C++;
there is no variable called "i" in scope for the "i = 0;"
statement.

-- James.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Dec 14, 2003 3:34 pm    Post subject: Re: Variables in for loop - language change? Reply with quote

In article <pqJCb.71009$_J.62395 (AT) newssvr25 (DOT) news.prodigy.com>, No Spam
<nospam (AT) myisp (DOT) net> writes
Quote:
Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Gnu compiler complains about this; Microsoft doesn't.

Under Gnu, must change to

int i;
for (i=0;i {
...
}
Looks to me as if you are using Gnu compiler in C89/90 mode.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Mantoulidis
Guest





PostPosted: Sun Dec 14, 2003 4:54 pm    Post subject: Re: Variables in for loop - language change? Reply with quote

Quote:
Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Of course it's allowed!!!

Quote:
Gnu compiler complains about this; Microsoft doesn't.

Well, then you've got an error. Not the compiler's fault...

Quote:
Under Gnu, must change to

int i;
for (i=0;i {
...
}

Nono!!!

You gotta use the second way if you want to use "i" in 2 or more for
loops... If you use if for just one loop, the 1st way works.

Example...

.....
int main()
{
//you CAN do that
for (int i = 0; i < 10; i++)
cout << "foo no. " << i << endl;
return 0;
}

2nd example:

.....
int main()
{
int i;
//here you canNOT do the above because you will use "i" in some
other
//loop
for (i = 0; i < 20; i++)
cout << "foo no. " << i < for (i = 4; i < 40; i++)
cout << "bar no. " << i << endl;
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Xenos
Guest





PostPosted: Mon Dec 15, 2003 10:05 pm    Post subject: Re: Variables in for loop - language change? Reply with quote


"Chris Mantoulidis" <cmad_x (AT) yahoo (DOT) com> wrote

Quote:
Is this way of declaring variables no longer allowed by the standard?

for (int i=0;i {
...
}

Of course it's allowed!!!

Gnu compiler complains about this; Microsoft doesn't.

Well, then you've got an error. Not the compiler's fault...

Under Gnu, must change to

int i;
for (i=0;i {
...
}

Nono!!!

You gotta use the second way if you want to use "i" in 2 or more for
loops... If you use if for just one loop, the 1st way works.

Example...

....
int main()
{
//you CAN do that
for (int i = 0; i < 10; i++)
cout << "foo no. " << i << endl;
return 0;
}

2nd example:

....
int main()
{
int i;
//here you canNOT do the above because you will use "i" in some
other
//loop
for (i = 0; i < 20; i++)
cout << "foo no. " << i < for (i = 4; i < 40; i++)
cout << "bar no. " << i << endl;
return 0;
}

Why do you think you need the second why to use i in multiple loops? You
only need to declare it outside the loop if you want to access it outside
the loop.

DrX




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kevin Saff
Guest





PostPosted: Tue Dec 16, 2003 8:50 pm    Post subject: Re: Variables in for loop - language change? Reply with quote

"Chris Mantoulidis" <cmad_x (AT) yahoo (DOT) com> wrote

Quote:

You gotta use the second way if you want to use "i" in 2 or more for
loops... If you use if for just one loop, the 1st way works.

Example...

[SNIP]

....
int main()
{
int i;
file://here you canNOT do the above because you will use "i" in some
other
file://loop
for (i = 0; i < 20; i++)
cout << "foo no. " << i < for (i = 4; i < 40; i++)
cout << "bar no. " << i << endl;
return 0;
}

No, you only have to do this in MSVC6. Maybe if you got a C++ compiler, you
wouldn't have these problems. :)

--
KCS




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Xenos
Guest





PostPosted: Thu Dec 18, 2003 8:47 am    Post subject: Re: Variables in for loop - language change? Reply with quote


"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote

Quote:
"Chris Mantoulidis" <cmad_x (AT) yahoo (DOT) com> wrote in message
news:a8587dd9.0312140616.678f76bc (AT) posting (DOT) google.com...
No, you only have to do this in MSVC6. Maybe if you got a C++ compiler,
you
wouldn't have these problems. :)

--
KCS
A simple, but kludgy, fix for the MSVC6 scoping problem with for loops is

this:

#define for if (0); else for



DrX




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Dec 19, 2003 11:16 am    Post subject: Re: Variables in for loop - language change? Reply with quote

Xenos <dont.spam.me (AT) spamhate (DOT) com> wrote

Quote:
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote in message
news:Hpzyrt.C1z (AT) news (DOT) boeing.com...
"Chris Mantoulidis" <cmad_x (AT) yahoo (DOT) com> wrote in message
news:a8587dd9.0312140616.678f76bc (AT) posting (DOT) google.com...
No, you only have to do this in MSVC6. Maybe if you got a C++
compiler, you wouldn't have these problems. :)

A simple, but kludgy, fix for the MSVC6 scoping problem with for loops
is this:

#define for if (0); else for

A much better solution is to just write clean code which is legal with
both C++ scoping rules. Most of the time, cleaning written,
understandable code will compiler equally well under both rules.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.