The below program may print some garbage data as string is stored in stack frame of function getString() and data may not be there after getString() returns.
#include <stdio.h>
char *getString()
{
  char str[] = "abc"; 
  return str; 
}     
int main()
{
  printf("%s", getString());  
  return 0;
}
Output : Garbage Value.
Then why this below program is running fine and not printing a garbage value.
#include<stdio.h>
int *fun()
{
    int i=50;
    int *p=&i;
    return p;
}
int main()
{
    int *q=fun();
    printf("%d",*q);
    return 0;
}
Output : 50
Here also p is a pointer in function fun() and is stored in stack segment. I am confused here. Why the output is 50 and not the garbage value ?
Thanks.
 
     
     
    