473,471 Members | 4,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Variable Scope

PSN
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

Jan 24 '07 #1
12 1580
PSN
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
Jan 24 '07 #2
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
Jan 24 '07 #3

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).

Jan 24 '07 #4

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).

Jan 24 '07 #5
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

Jan 24 '07 #6

"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
Jan 24 '07 #7
*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
Jan 24 '07 #8
*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)
Jan 24 '07 #9

"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
Jan 24 '07 #10
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.
Jan 25 '07 #11
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.

Jan 25 '07 #12

"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.
Jan 26 '07 #13

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

Similar topics

7
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...
3
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...
4
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...
23
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) { ..... }
1
pbmods
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...
0
MMcCarthy
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...
2
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...
5
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;
112
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...
3
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...
0
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...
0
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...
0
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
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...
0
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...
0
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,...
1
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
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 ...
0
muto222
php
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.