@siddhartha You are absolutely right that it should print the garbage value. But it is printing 10.
Here is the reason. This program is printing 10, because you are running this program in the windows environment. Windows does not clear the memory when the stack gets deleted. It just deletes the reference. What happens that you have the address in P, which is pointing to value 10, which has not been replaced by any other program, hence you are getting the 10.
Run this program in Linux environment and i am 100% sure you will get 0 as output. In the Linux, when a stack gets deleted it reset all the memory as 0. Hence you get 0.
Here, I run this program at the ideone.com which is a Linux based system. I get 0.