Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 02-10-2008, 02:44 PM   #1 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default 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/
redhat is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-10-2008, 03:01 PM   #2 (permalink)
Back!
 
red_devil's Avatar
 
Join Date: Jun 2007
Location: Bangalore
Posts: 513
Default 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 ?
red_devil is offline  
Old 02-10-2008, 03:11 PM   #3 (permalink)
=--=l33t=--=
 
ring_wraith's Avatar
 
Join Date: Oct 2007
Location: In Limbo
Posts: 722
Default 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.
ring_wraith is offline  
Old 02-10-2008, 03:37 PM   #4 (permalink)
Back!
 
red_devil's Avatar
 
Join Date: Jun 2007
Location: Bangalore
Posts: 513
Default 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 ??
red_devil is offline  
Old 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
Default Re: Scope of a variable in C++

^^Yes it happens in Turbo C v 3
ThinkFree is offline  
Old 02-10-2008, 04:48 PM   #6 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default 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
dheeraj_kumar is offline  
Old 02-10-2008, 05:39 PM   #7 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default Re: Scope of a variable in C++

Quote:
Originally Posted by dheeraj_kumar View Post
^^ Seconded. Ive found this error really annoying in Turbo C too.
Error ?? You mean BUG in Turbo C??
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is offline  
Old 02-10-2008, 05:41 PM   #8 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 147
Default 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
sreenidhi88 is offline  
Old 02-10-2008, 05:48 PM   #9 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default 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.
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is offline  
Old 02-10-2008, 05:51 PM   #10 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default 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
chandru.in is offline  
Old 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
Default Re: Scope of a variable in C++

^^+1
ThinkFree is offline  
Old 02-10-2008, 07:02 PM   #12 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Scope of a variable in C++

Quote:
Originally Posted by redhat View Post
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
Abhishek Dwivedi is offline  
Old 02-10-2008, 07:08 PM   #13 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Scope of a variable in C++

Quote:
Originally Posted by Abhishek Dwivedi View Post
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
chandru.in is offline  
Old 02-10-2008, 07:24 PM   #14 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Scope of a variable in C++

Quote:
Originally Posted by chandru.in View Post
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
Abhishek Dwivedi is offline  
Old 02-10-2008, 07:27 PM   #15 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Scope of a variable in C++

Quote:
Originally Posted by Abhishek Dwivedi View Post
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
chandru.in is offline  
Old 05-10-2008, 12:10 AM   #16 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default 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/
redhat is offline  
Old 05-10-2008, 10:05 AM   #17 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Scope of a variable in C++

Quote:
Originally Posted by redhat View Post
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
QwertyManiac is offline  
Old 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
Default 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.
ThinkFree is offline  
Old 05-10-2008, 01:29 PM   #19 (permalink)
In The Zone
 
ayush_chh's Avatar
 
Join Date: Nov 2005
Location: Bangalore
Posts: 487
Default 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.....
ayush_chh is offline  
Old 05-10-2008, 01:30 PM   #20 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Scope of a variable in C++

^^ GCC
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 05-10-2008, 01:48 PM   #21 (permalink)
In The Zone
 
ayush_chh's Avatar
 
Join Date: Nov 2005
Location: Bangalore
Posts: 487
Default 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.....
ayush_chh is offline  
Old 05-10-2008, 02:08 PM   #22 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Scope of a variable in C++

http://gcc.gnu.org/install/specific.html#windows
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 05-10-2008, 02:10 PM   #23 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 07-10-2008, 08:19 PM   #24 (permalink)
In The Zone
 
ayush_chh's Avatar
 
Join Date: Nov 2005
Location: Bangalore
Posts: 487
Default Re: Scope of a variable in C++

^^ thank you for the link...
__________________
eXPerience is what a MAN learn's fROM.....
ayush_chh is offline  
Old 07-10-2008, 08:47 PM   #25 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default 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/
redhat is offline  
Old 07-10-2008, 08:53 PM   #26 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swap two numbers without using a temporary variable PcEnthu Programming 24 29-04-2008 11:56 PM
prob wid double variable prancks Programming 5 08-04-2008 04:11 PM
how add environment variable in ubuntu mak1012 Open Source 17 17-09-2007 05:48 PM
cant declare a string variable... geekgod Open Source 13 12-08-2006 06:09 AM
In Excel How to use cell ref as a variable abhijitroy Software Q&A 1 10-07-2006 02:32 PM

 
Latest Threads
- by clinton
- by soumya
- by Sujeet
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 11:13 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2