# Scope of a variable in C++



## redhat (Oct 2, 2008)

I am new to programming in C++, although I have done Java
Recently, while writing a C++ program, I used two loops as follows:

```
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 (Oct 2, 2008)

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 ?


----------



## ring_wraith (Oct 2, 2008)

I had a similar issue. Funnily enough, the same code ran well on RELO.


----------



## red_devil (Oct 2, 2008)

How is that possible ??

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


----------



## ThinkFree (Oct 2, 2008)

^^Yes it happens in Turbo C v 3


----------



## dheeraj_kumar (Oct 2, 2008)

^^ Seconded. Ive found this error really annoying in Turbo C too.


----------



## Garbage (Oct 2, 2008)

dheeraj_kumar said:


> ^^ Seconded. Ive found this error really annoying in Turbo C too.


Error ?? You mean BUG in Turbo C??


----------



## sreenidhi88 (Oct 2, 2008)

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


----------



## Garbage (Oct 2, 2008)

^^ actually, scope of i MUST be for first loop only...

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


----------



## chandru.in (Oct 2, 2008)

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.


----------



## ThinkFree (Oct 2, 2008)

^^+1


----------



## Abhishek Dwivedi (Oct 2, 2008)

redhat said:


> I am new to programming in C++, although I have done Java
> Recently, while writing a C++ program, I used two loops as follows:
> 
> ```
> ...



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 (Oct 2, 2008)

Abhishek Dwivedi said:


> 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 (Oct 2, 2008)

chandru.in said:


> 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 (Oct 2, 2008)

Abhishek Dwivedi said:


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


----------



## redhat (Oct 5, 2008)

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 (Oct 5, 2008)

redhat said:


> 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 (Oct 5, 2008)

^^Right. If students keep on following there schools/colleges prescription, they can't be able to learn the latest features of any language.


----------



## ayush_chh (Oct 5, 2008)

then.......can any 1 pls suggest me a good modern compiler...


----------



## chandru.in (Oct 5, 2008)

^^ *GCC*


----------



## ayush_chh (Oct 5, 2008)

can u pls tell how can i d/l it........ for windows


----------



## chandru.in (Oct 5, 2008)

*gcc.gnu.org/install/specific.html#windows


----------



## QwertyManiac (Oct 5, 2008)

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.


----------



## ayush_chh (Oct 7, 2008)

^^ thank you for the link...


----------



## redhat (Oct 7, 2008)

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


----------



## QwertyManiac (Oct 7, 2008)

When you know what the differences are, I don't see how that problem could be for real.


----------

