 |
02-10-2008, 02:44 PM
|
#1 (permalink)
|
|
Mad and Furious
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
|
Scope of a variable in C++
I am new to programming in C++, although I have done Java
Recently, while writing a C++ program, I used two loops as follows:
Code:
for(int i=0, i<=n; i=i+1)
{
//statements
}
for(int i=0; i<=m; i=i+1)
{
/statements
}
// where n and m are predefined variables
I am using the Turbo C V3 compiler, since my college requires me to do so....
Now my problem is that, the compiler gave me an error in the 2nd loop saying that variable i is already defined. Upon changing the variable name in the 2nd loop, my program ran well. I suppose the scope of variable "i" should have ended before the 2nd loop, why did it give me such an error?
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
02-10-2008, 03:01 PM
|
#2 (permalink)
|
|
Back!
Join Date: Jun 2007
Location: Bangalore
Posts: 513
|
Re: Scope of a variable in C++
err..you should be able to use "i" in the second for loop without any hassles !!
can you post the complete code you were trying to execute ?
|
|
|
02-10-2008, 03:11 PM
|
#3 (permalink)
|
|
=--=l33t=--=
Join Date: Oct 2007
Location: In Limbo
Posts: 722
|
Re: Scope of a variable in C++
I had a similar issue. Funnily enough, the same code ran well on RELO.
__________________
Homer: God bless those pagans.
|
|
|
02-10-2008, 03:37 PM
|
#4 (permalink)
|
|
Back!
Join Date: Jun 2007
Location: Bangalore
Posts: 513
|
Re: Scope of a variable in C++
How is that possible ??
wait ... has it got anything to do with the highly (in)famous TURBO C compiler ??
|
|
|
02-10-2008, 04:00 PM
|
#5 (permalink)
|
|
Human Spambot
Join Date: Jan 2007
Location: Lat 28.38°N , Longt 77.13°E
Posts: 2,431
|
Re: Scope of a variable in C++
^^Yes it happens in Turbo C v 3
|
|
|
02-10-2008, 04:48 PM
|
#6 (permalink)
|
|
Legen-wait for it-dary!
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
|
Re: Scope of a variable in C++
^^ Seconded. Ive found this error really annoying in Turbo C too.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
|
|
|
02-10-2008, 05:39 PM
|
#7 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: Scope of a variable in C++
Quote:
Originally Posted by dheeraj_kumar
^^ Seconded. Ive found this error really annoying in Turbo C too.
|
Error ?? You mean BUG in Turbo C??
|
|
|
02-10-2008, 05:41 PM
|
#8 (permalink)
|
|
Broken In
Join Date: Sep 2006
Posts: 147
|
Re: Scope of a variable in C++
am i missing something here?
why are you redeclaring the variable "i" in the second loop?
once you have declared the variable i,memory is allocated and the memory location is reserved for the entire program duration.
for (int i ......)
for(just i (dont declare here)....) 
__________________
techtricks.co.in
portforwarding
xp-vista crossover connection
installing xp on vista notebooks
|
|
|
02-10-2008, 05:48 PM
|
#9 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: Scope of a variable in C++
^^ actually, scope of i MUST be for first loop only...
Code is having no errors... Try compiling the code using gcc.
|
|
|
02-10-2008, 05:51 PM
|
#10 (permalink)
|
|
CAFEBABE
Join Date: Mar 2008
Location: Bangalore
Posts: 474
|
Re: Scope of a variable in C++
Turbo C++ is a dinosaur compiler. So it implements the older C++ standard, in which variables defined within loop statements were available after the completion of loops block too.
However, with the latest ISO C++ standard (which most modern compilers implement), this does not work so. The code posted by OP is the right and modern way of doing things.
It is also not a Turbo C++ bug. It is just that it is too old and weak.
__________________
Chandru
http://tuxychandru.blogspot.com
|
|
|
02-10-2008, 05:51 PM
|
#11 (permalink)
|
|
Human Spambot
Join Date: Jan 2007
Location: Lat 28.38°N , Longt 77.13°E
Posts: 2,431
|
Re: Scope of a variable in C++
^^+1
|
|
|
02-10-2008, 07:02 PM
|
#12 (permalink)
|
|
TechFreakiez.com
Join Date: Sep 2006
Location: New Delhi
Posts: 621
|
Re: Scope of a variable in C++
Quote:
Originally Posted by redhat
I am new to programming in C++, although I have done Java
Recently, while writing a C++ program, I used two loops as follows:
Code:
for(int i=0, i<=n; i=i+1)
{
//statements
}
for(int i=0; i<=m; i=i+1)
{
/statements
}
// where n and m are predefined variables
I am using the Turbo C V3 compiler, since my college requires me to do so....
Now my problem is that, the compiler gave me an error in the 2nd loop saying that variable i is already defined. Upon changing the variable name in the 2nd loop, my program ran well. I suppose the scope of variable "i" should have ended before the 2nd loop, why did it give me such an error?
|
this happens 'cause TC is an old compiler and it does not accpts re-declaration of any pre-declared variable even if its scope is over...on the othr hand re-declaration works pretty well whn u define the variable in diffrnt class and/or function as in that case the variable have diffrnt scope
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
|
|
|
02-10-2008, 07:08 PM
|
#13 (permalink)
|
|
CAFEBABE
Join Date: Mar 2008
Location: Bangalore
Posts: 474
|
Re: Scope of a variable in C++
Quote:
Originally Posted by Abhishek Dwivedi
it does not accpts re-declaration of any pre-declared variable even if its scope is over...
|
Just a slight correction, it is not about accepting redeclaration after scope is over. It is about the scope of the variable itself.
For example, if you print value of i, immediately after the first loop in Turbo C, it will print the value as n+1. So the variable is still in scope there and no compiler allows redeclaring an identifier when it already exists with same scope.
__________________
Chandru
http://tuxychandru.blogspot.com
|
|
|
02-10-2008, 07:24 PM
|
#14 (permalink)
|
|
TechFreakiez.com
Join Date: Sep 2006
Location: New Delhi
Posts: 621
|
Re: Scope of a variable in C++
Quote:
Originally Posted by chandru.in
Just a slight correction, it is not about accepting redeclaration after scope is over. It is about the scope of the variable itself.
For example, if you print value of i, immediately after the first loop in Turbo C, it will print the value as n+1. So the variable is still in scope there and no compiler allows redeclaring an identifier when it already exists with same scope.
|
hmm..so basically the problem is that the variable is still in the scope...well, ur right...and if its in scope, thn no compiler shud allow it to be declared again...
but does this wrks in other compilers of C++ ??
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
|
|
|
02-10-2008, 07:27 PM
|
#15 (permalink)
|
|
CAFEBABE
Join Date: Mar 2008
Location: Bangalore
Posts: 474
|
Re: Scope of a variable in C++
Quote:
Originally Posted by Abhishek Dwivedi
hmm..so basically the problem is that the variable is still in the scope...well, ur right...and if its in scope, thn no compiler shud allow it to be declared again...
but does this wrks in other compilers of C++ ??
|
This will work in any C++ compiler which implements the same C++ standard as TC. But most modern C++ compilers implementing the C++ ISO 99 standard will not do it this way, as the new standard has changed the scoping for such definitions.
__________________
Chandru
http://tuxychandru.blogspot.com
|
|
|
05-10-2008, 12:10 AM
|
#16 (permalink)
|
|
Mad and Furious
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
|
Re: Scope of a variable in C++
Well, the problem happens to be that the state board has prescribed Turbo C as the default compiler, so all code has to be TC compilant....
Thanks for resolving my doubt.. Atleast know i know, my logic wasnt wrong, its just old standards..
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
|
|
|
05-10-2008, 10:05 AM
|
#17 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Scope of a variable in C++
Quote:
Originally Posted by redhat
Well, the problem happens to be that the state board has prescribed Turbo C as the default compiler, so all code has to be TC compilant....
Thanks for resolving my doubt.. Atleast know i know, my logic wasnt wrong, its just old standards..
|
Even if they have prescribed it, its not really necessary for you to use the same for learning C/C++ at home. You will learn to handle pointers much better on modern non-forgiving compilers than on TC and such, and also learn many new features such as the STL and Boost (If you install it).
__________________
Harsh J
www.harshj.com
|
|
|
05-10-2008, 10:53 AM
|
#18 (permalink)
|
|
Human Spambot
Join Date: Jan 2007
Location: Lat 28.38°N , Longt 77.13°E
Posts: 2,431
|
Re: Scope of a variable in C++
^^Right. If students keep on following there schools/colleges prescription, they can't be able to learn the latest features of any language.
|
|
|
05-10-2008, 01:29 PM
|
#19 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: Scope of a variable in C++
then.......can any 1 pls suggest me a good modern compiler...
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
05-10-2008, 01:30 PM
|
#20 (permalink)
|
|
CAFEBABE
Join Date: Mar 2008
Location: Bangalore
Posts: 474
|
Re: Scope of a variable in C++
__________________
Chandru
http://tuxychandru.blogspot.com
|
|
|
05-10-2008, 01:48 PM
|
#21 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: Scope of a variable in C++
can u pls tell how can i d/l it........ for windows
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
05-10-2008, 02:08 PM
|
#22 (permalink)
|
|
CAFEBABE
Join Date: Mar 2008
Location: Bangalore
Posts: 474
|
Re: Scope of a variable in C++
__________________
Chandru
http://tuxychandru.blogspot.com
|
|
|
05-10-2008, 02:10 PM
|
#23 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Scope of a variable in C++
The good approach would be to download an IDE like Dev-C++ with mingw32 from Bloodshed.net
(mingw32 is 32-bit Minimalistic GNU for Windows - Includes various development tools including basic compiler and associated libraries.)
Else just download mingw32 from SourceForge and use command-line to compile.
__________________
Harsh J
www.harshj.com
|
|
|
07-10-2008, 08:19 PM
|
#24 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: Scope of a variable in C++
^^ thank you for the link...
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
07-10-2008, 08:47 PM
|
#25 (permalink)
|
|
Mad and Furious
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
|
Re: Scope of a variable in C++
I personally often use VC++, but the answers i write in my paper have to be TC compilant, and in such a case, my answer would be marked wrong!! Hence, i use TC
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
|
|
|
07-10-2008, 08:53 PM
|
#26 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Scope of a variable in C++
When you know what the differences are, I don't see how that problem could be for real.
__________________
Harsh J
www.harshj.com
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|