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