Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Variable Scope

Reply
Thread Tools

Variable Scope

 
 
PSN
Guest
Posts: n/a
 
      01-24-2007
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

 
Reply With Quote
 
 
 
 
PSN
Guest
Posts: n/a
 
      01-24-2007
The MSVC++ compiler ends up with a redefinition error.

On Jan 24, 3:17 pm, "PSN" <(E-Mail Removed)> 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;
>
> }Thanks,
> Prakash


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-24-2007
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


 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-24-2007

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

 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-24-2007

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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-24-2007
On Jan 24, 9:19 am, "PSN" <(E-Mail Removed)> wrote:
> On Jan 24, 3:17 pm, "PSN" <(E-Mail Removed)> 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;

>
> > }

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

 
Reply With Quote
 
*PaN!*
Guest
Posts: n/a
 
      01-24-2007

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:ep7q17$31s$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-24-2007
*PaN!* wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:ep7q17$31s$(E-Mail Removed)...
>> 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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-24-2007
*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)
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      01-24-2007
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments