I thought that once a function returns, all the local variables declared within (barring those with static keyword) are garbage collected. But when I am trying
There's no garbage collection in C. Once the scope of a variable cease to exist, accessing it in any means is illegal. What you see is UB(Undefined behaviour).