DIGIT-ARC read-only archive · snapshot: July 23, 2026

Scope of a variable in C++

26 posts · originally from https://geek.digit.in/community/threads/scope-of-a-variable-in-c.98907/

redhat
redhat Original post #1
Oct 2, 2008 at 2:44 PM
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
Oct 2, 2008 at 3:01 PM
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 at 3:11 PM
I had a similar issue. Funnily enough, the same code ran well on RELO.
red_devil
Oct 2, 2008 at 3:37 PM
How is that possible ??

wait ... has it got anything to do with the highly (in)famous TURBO C compiler ??
T
Oct 2, 2008 at 4:00 PM
^^Yes it happens in Turbo C v 3
dheeraj_kumar
Oct 2, 2008 at 4:48 PM
^^ Seconded. Ive found this error really annoying in Turbo C too.
S
Oct 2, 2008 at 5:41 PM
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
Garbage #9
Oct 2, 2008 at 5:48 PM
^^ actually, scope of i MUST be for first loop only...

Code is having no errors... Try compiling the code using gcc.
C
Oct 2, 2008 at 5:51 PM
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.
T
ThinkFree #11
Oct 2, 2008 at 5:51 PM
^^+1
Abhishek Dwivedi
Oct 2, 2008 at 7:02 PM
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
C
Oct 2, 2008 at 7:08 PM
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 at 7:24 PM
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++ ??
C
Oct 2, 2008 at 7:27 PM
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
redhat #16
Oct 5, 2008 at 12:10 AM
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 at 10:05 AM
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).
T
ThinkFree #18
Oct 5, 2008 at 10:53 AM
^^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
ayush_chh #19
Oct 5, 2008 at 1:29 PM
then.......can any 1 pls suggest me a good modern compiler...:)
ayush_chh
ayush_chh #21
Oct 5, 2008 at 1:48 PM
can u pls tell how can i d/l it........ for windows
C
Oct 5, 2008 at 2:08 PM
*gcc.gnu.org/install/specific.html#windows
QwertyManiac
Oct 5, 2008 at 2:10 PM
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
ayush_chh #24
Oct 7, 2008 at 8:19 PM
^^ thank you for the link...:)
redhat
redhat #25
Oct 7, 2008 at 8:47 PM
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 at 8:53 PM
When you know what the differences are, I don't see how that problem could be for real.