The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}
Thanks,
Prakash 12 1580
The MSVC++ compiler ends up with a redefinition error.
On Jan 24, 3:17 pm, "PSN" <prakash...@gmail.comwrote:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}Thanks,
Prakash
PSN wrote:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}
No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :-)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
PSN napsal:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}
Thanks,
Prakash
AFAIK it was changed (I do not how many years ago) in standard, so that
'i' is valid only within loop. In some howto for Mozilla (how to write
portable code) was mentioned, that some compilers will not compile your
example. But I think it is correct acording to standard (it is not
redefinition).
PSN napsal:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}
Thanks,
Prakash
AFAIK it was changed (I do not how many years ago) in standard, so that
'i' is valid only within loop. In some howto for Mozilla (how to write
portable code) was mentioned, that some compilers will not compile your
example. But I think it is correct acording to standard (it is not
redefinition).
On Jan 24, 9:19 am, "PSN" <prakash...@gmail.comwrote:
On Jan 24, 3:17 pm, "PSN" <prakash...@gmail.comwrote:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...
main()
{
for (int i=0; i<10; i++)
cout << i << endl;
for (int i=10; i>0; i--)
cout << i << endl;
}
The MSVC++ compiler ends up with a redefinition error.
VC6 is not conformant on this point (MSDN has a note on this). The
standard allows it, as do all C++ compilers released in the last decade
or so.
Cheers! --M
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ep**********@news.datemas.de...
PSN wrote:
>The following code compiles fine with GCC. Isnt this supposed to be a redefinition of 'i' error ...
main() { for (int i=0; i<10; i++) cout << i << endl;
for (int i=10; i>0; i--) cout << i << endl; }
No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :-)
btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >(it can be easily set as a
project-level compiler switch).
--
Marco
*PaN!* wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ep**********@news.datemas.de...
>PSN wrote:
>>The following code compiles fine with GCC. Isnt this supposed to be a redefinition of 'i' error ...
main() { for (int i=0; i<10; i++) cout << i << endl;
for (int i=10; i>0; i--) cout << i << endl; }
No. That's the rule for variables declared in the 'for' statement. Before it was standardized that way the language went back and forth a couple of times briefly, and some compilers (VC++) implemented it the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler and perhaps you've got used to the incorrect 'for' variables rule... It's time to _un-learn_ bad habits. :-)
btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >(it can be easily set as a
project-level compiler switch).
It's an ugly hack, and is against the Standard as well, BTW. Of
course keeping using VC++ v6 nowadays is a punishable offence (or
it should be :-))
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
*PaN!* wrote:
>
btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >(it can be easily set as a
project-level compiler switch).
This introduces undefined behavior if you're not extremely careful. In
every situation I've seen where the same name was used for the loop
variable in multiple for loops, the simplest workaround was to hoist the
declaration of the variable into the enclosing block:
int main()
{
for (int i = 0; i < 10; ++i)
;
for (int i = 0; i < 10; ++i)
;
return 0;
}
becomes
int main()
{
int i;
for (i = 0; i < 10; ++i)
;
for (i = 0; i < 10; ++i)
;
return 0;
}
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:du******************************@giganews.com ...
>btw, most of the people I know use a workaround for VC++ 6, that is re-defining for as << if(0);else for >(it can be easily set as a project-level compiler switch).
This introduces undefined behavior if you're not extremely careful.
I never trusted that solution very much (and it's really ugly), but can't
see how an undefined behaviour can arise (except if someone else is already
redefining for).
In every situation I've seen where the same name was used for the loop
variable in multiple for loops, the simplest workaround was to hoist the
declaration of the variable into the enclosing block:
Indeed, my favourite solution is modifying the code.
int main()
{
for (int i = 0; i < 10; ++i)
;
for (int i = 0; i < 10; ++i)
;
return 0;
}
becomes
int main()
{
int i;
for (i = 0; i < 10; ++i)
;
for (i = 0; i < 10; ++i)
;
return 0;
}
My favourite solution is actually
int main()
{
{
for (int i = 0; i < 10; ++i)
;
}
{
for (int i = 0; i < 10; ++i)
;
}
}
since while refactoring the code it might happen
a) that the two loops have different types for the i variable (e.g. int,
unsigned int)
b) that the two loops are in a longer/complex function
so I found that a simpler/faster solution is to enclose the for-loops in a
block.
--
Marco
In article <11**********************@q2g2000cwa.googlegroups. com>, ml*****@gmail.com says...
[ ... ]
VC6 is not conformant on this point (MSDN has a note on this). The
standard allows it, as do all C++ compilers released in the last decade
or so.
It's mostly a technicality, but VC++ 6 _can_ conform in this regard --
if you ask it for its best conformance with the standard (/Za) it gives
variables declared in for loops the correct scope.
As I said, this is mostly a technicality though: when you do this, it
also enforces a couple of other rules that prevent it from compiling
nearly _any_ of its own headers, so the ability is generally useless
even though it's present.
--
Later,
Jerry.
The universe is a figment of its own imagination.
No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :-)
We fixed it with a macro in the VC6. Even the contemporaneous
Microsoft doc's claimed it a "Microsoft Extension" before the
standard was published. The problem in VC6 is you can't turn
it off (i.e., get the standard behavior) without disabliing a
lot of other "extensions" that the development environment
relied on.
The later versions of Visual C++ (honestly, VC6 is two major
relases and nearly ten years old now) allow you to fix the
for loops with an option (which I believe is the 'standard'
way by default) distinct from anything else.
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45**********************@news.newshosting.com ...
The later versions of Visual C++ (honestly, VC6 is two major
relases and nearly ten years old now) allow you to fix the
for loops with an option (which I believe is the 'standard'
way by default) distinct from anything else.
In VC7.1, at least, it's not the standard way by default
but at least you can set it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: YGeek |
last post by:
Is there any difference between declaring a variable at the top of a method
versus in the code of the method? Is there a performance impact for either
choice? What about if the method will return...
|
by: Grant Wagner |
last post by:
Given the following working code:
function attributes() {
var attr1 = arguments || '_';
var attr2 = arguments || '_';
return (
function (el1, el2) {
var value1 = el1 + el1;
var value2 = el2...
|
by: Gery D. Dorazio |
last post by:
Gurus,
If a static variable is defined in a class what is the scope of the variable
resolved to for it to remain 'static'? For instance, lets say I create a
class library assembly that is...
|
by: Russ Chinoy |
last post by:
Hi,
This may be a totally newbie question, but I'm stumped.
If I have a function such as:
function DoSomething(strVarName) {
.....
}
|
by: pbmods |
last post by:
VARIABLE SCOPE IN JAVASCRIPT
LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN )
PREREQS: VARIABLES
First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
|
by: MMcCarthy |
last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
|
by: Shraddha |
last post by:
Can we declare extern variable as static?
What will be the scope of the variable then? What if we change the
value of the variable in some other function?
Also can someone tell me that if we can...
|
by: somenath |
last post by:
Hi All ,
I have one question regarding scope and lifetime of variable.
#include <stdio.h>
int main(int argc, char *argv)
{
int *intp = NULL;
char *sptr = NULL;
|
by: istillshine |
last post by:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions...
|
by: SRoubtsov |
last post by:
Dear all,
Do you know whether ANSI C (or some other dialects) support the
following:
* a variable name coincides with a type name,
* a structure/union field name coincides with a type name
in...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |