Scope of a variable in C++

Status
Not open for further replies.

redhat

Mad and Furious
Joined
May 28, 2006
Messages
450
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?
 

red_devil

Back!
Joined
Jun 2, 2007
Messages
503
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

Back!
Joined
Jun 2, 2007
Messages
503
How is that possible ??

wait ... has it got anything to do with the highly (in)famous TURBO C compiler ??
 

sreenidhi88

Journeyman
Joined
Sep 14, 2006
Messages
145
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)....):grin::))
 

Garbage

God of Mistakes...
Joined
Dec 26, 2005
Messages
1,896
^^ actually, scope of i MUST be for first loop only...

Code is having no errors... Try compiling the code using gcc.
 

chandru.in

In the zone
Joined
Mar 23, 2008
Messages
472
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.
 

Abhishek Dwivedi

TechFreakiez.com
Joined
Sep 28, 2006
Messages
614
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
 

chandru.in

In the zone
Joined
Mar 23, 2008
Messages
472
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.
 

Abhishek Dwivedi

TechFreakiez.com
Joined
Sep 28, 2006
Messages
614
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++ ??
 

chandru.in

In the zone
Joined
Mar 23, 2008
Messages
472
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.
 
OP
redhat

redhat

Mad and Furious
Joined
May 28, 2006
Messages
450
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..
 

QwertyManiac

Commander in Chief
Joined
Jul 17, 2005
Messages
6,575
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).
 

ThinkFree

Technomancer
Joined
Jan 11, 2007
Messages
2,403
^^Right. If students keep on following there schools/colleges prescription, they can't be able to learn the latest features of any language.
 
Status
Not open for further replies.
Top