#include<stdio.h>
int * show();
int main()
{
int *p=show();
printf("%d",*p); //Referring x that is de-allocated now but it's value is still exist on memory(in stack)
printf("%d",*p); //The memory (stack space) which is earlier allocated to show() is overwritten now by above printf so it will print a garbage value
return 0;
}
int * show()
{
int x=10;
return &x;
} //e
Although scope and lifetime of variable x end as point e but its value is still there on the stack that's why 1st printf gives correct output. But after that, the memory is overwritten so 2nd printf will return a garbage value.
NOTE -
GCC compiler returns 10 -2(this is a garbage value) with a Warning:
function returns address of local variable.