This question refers to the automatic storage class and its scope is defined as local to the block in which the variable is defined and is alive until the control is present in that block where it is defined. Consider these two snippets
...

main( )
{
auto int i = 1 ;
{
{
{
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
}

The output of the following would be 111

main()

{
auto int i = 1 ;
{

auto int i = 2 ;
{
auto int i = 3 ;
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
}

The output of the following is 321

For the second snippet I don't understand how the output is 321 as all the three ints are active (scope wise and alive) when the first printf is executed. How is the computer reporting a 3? Shouldn't there be a conflict? auto int i=1 is defined in the outermst block and the control is present in that block hence it is alive and the value is retained, the same goes for auto int i=2 as well as 3. Regarding the scope which is defined as over what parts of the program the variable is available for use, the scope is very much in the innermost block i.e. for the first printf. Why such an output. I have posteed the first snippet as it contradicts with the above observation. The output in the first is all 1's, meaning the auto int i=1 is alive and has scope in all the inner blocks? Why is the output in the second program 321???

Sorry for such a long post! Thanks in advance

Recommended Answers

All 6 Replies

dude when u redefine it as local variable then global variable has no more effect and when u print the same then the local variable u defines is printed ...in ur second case when u define int=3 ,then it is local to that block and when this block get executed so it print 3 .....now when this block executed then int i =3 is deleted from stack and now in memory we have int i=2;and so on ..

Shouldn't there be a conflict?

No. I agree, that you could create a programming langage in which you wouldn't be allowed to create two objects with the same name in the way that you can in C and C++, but that's not how C and C++ were designed.

In C and C++, if you create a new scope inside an existing scope, you can have a new variable in there with the same name as one in the outer scope, and you'll use that new one instead while you're inside that inner scope. If you don't create a new variable with the same name, you go on using the existing one. That's the rule. This effect is typically called "shadowing".

There is no conflict because that's how the language was defined. Those are the rules of the language. Now you've learnt one of the rules. Well done; only another million rules to go. :)

Another term for this is "scoping", in that the scope of the variable is until the block it is defined within terminates. That particular variable will take precidence over others of the same name that are defined outside of the block, including global variables.

hey thanks for your replies! So I was analyzing it all wrong! It is a rule then rather than an inference of something already established/defined i.e. the scope/life of a variable! Thanks once again!!

Yup, variables in a nested scope hide variables with the same name in parent scopes.

Glad to help. Sometimes basic C/C++ programming rules can be rather confusing, even to us "experts". :-)

commented: I want the day when I can say myself "expert" +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.