Here and Here I found that variables in block are created when execution reaches that block,
To prove that I tried this:
int main()
{
{
char a;
printf("Address of a %d \n",&a);
}
char b;
printf("Address of b %d \n",&b);
}
As expected b was created first (because outer block is executed sooner than inner), and when the execution reached inner block a was created. Output of the above code:
Address of a 2686766
Address of b 2686767
(Tested on x86 (stack grows downwards, so variable with greater address was created first)).
But what about this one?:
int main()
{
{
char a;
printf("Address of a %d \n",&a);
} // I expected that variable a should be destroyed here
{
char b;
printf("Address of b %d \n",&b);
}
}
Output:
Address of a 2686767
Address of b 2686766
I expected that a was destroyed at closing brace of the first block statement, so address where a was located is now top of stack, and b should be created here, thus in the Output above both addresses should be equal, but they aren't? Are variables destroyed at the end of block? If not, why?