www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why is a variable allowed to be declared twice?

reply jicman <jicman_member pathlink.com> writes:
As in,

import std.stdio;
void main()
{
int i;
for (int i=0;i<2;i++)
writefln(i);
}

Should not have the compiler complained about it being declared twice?

I missed this one and it caused me. :-)

thanks,

josé
Mar 08 2005
parent reply brad domain.invalid writes:
jicman wrote:
 As in,
 
 import std.stdio;
 void main()
 {
 int i;
 for (int i=0;i<2;i++)
 writefln(i);
 }
 
 Should not have the compiler complained about it being declared twice?
 
 I missed this one and it caused me. :-)
 
 thanks,
 
 josé
 
 
No, the int i inside the for loop has a different scope. This is the same as C++, and infact g++ doesn't even warn about i being hiding with -Wall on. On a standards compilant C++ compiler (which MSVC by default is not) this will break <code> for (int i=0; i < 10; i++) ; printf("%i\n", i); // should be an error because i is no longer in scope. </code> D has the same scoping rules for "for" loops. Brad
Mar 08 2005
next sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
Most/all decent C++ compilers warn about this ...


<brad domain.invalid> wrote in message 
news:d0lrc9$12oo$1 digitaldaemon.com...
 jicman wrote:
 As in,

 import std.stdio;
 void main()
 {
 int i;
 for (int i=0;i<2;i++)
 writefln(i);
 }

 Should not have the compiler complained about it being declared 
 twice?

 I missed this one and it caused me. :-)

 thanks,

 josé
No, the int i inside the for loop has a different scope. This is the same as C++, and infact g++ doesn't even warn about i being hiding with -Wall on. On a standards compilant C++ compiler (which MSVC by default is not) this will break <code> for (int i=0; i < 10; i++) ; printf("%i\n", i); // should be an error because i is no longer in scope. </code> D has the same scoping rules for "for" loops. Brad
Mar 08 2005
prev sibling parent reply Ant <duitoolkit yahoo.ca> writes:
On Wed, 09 Mar 2005 16:43:34 +1300, brad wrote:

but this looks like it shouldn't be valid
(http://www.digitalmars.com/d/statement.html#block):

import std.stdio;
void main(char[][] args)
{
	int i = 25;
	{
		int args;
	}
	{
		int i = 15;
		writefln("in ",i);
	}
	for (int i=0; i<2; i++)
	{
		writefln("for ",i);
		for (int i=425; i<427; i++)
		{
			writefln("\tfor ",i);
		}
	}
	writefln("out ",i);
}


#outputs
in 15
for 0
	for 425
	for 426
for 1
	for 425
	for 426
out 25


Ant
Mar 08 2005
parent jicman <jicman_member pathlink.com> writes:
yeah, well, I was right... :-)

Ant says...
On Wed, 09 Mar 2005 16:43:34 +1300, brad wrote:

but this looks like it shouldn't be valid
(http://www.digitalmars.com/d/statement.html#block):

import std.stdio;
void main(char[][] args)
{
	int i = 25;
	{
		int args;
	}
	{
		int i = 15;
		writefln("in ",i);
	}
	for (int i=0; i<2; i++)
	{
		writefln("for ",i);
		for (int i=425; i<427; i++)
		{
			writefln("\tfor ",i);
		}
	}
	writefln("out ",i);
}


#outputs
in 15
for 0
	for 425
	for 426
for 1
	for 425
	for 426
out 25


Ant
Mar 09 2005