C program - explain output of following program

char *fun()
{
char *tmp = “SOME TEXT”;
return tmp;
}

void main()
{
puts(fun());
}

u need to use char pointer

char* fun()
{
const char* tmp="SOME TEXT";
return tmp;
}

void main()
{
puts(tmp);
}
  • little correction to the above program in which ranjan81 uses a pointer

char* fun()
{
const char* tmp=“SOME TEXT”;
return tmp;
}

void main()
{
puts(fun());// temp cant be used here
}

use pointer to store the value of string…

some garbage value is printed (or maybe segfault) since “temp” is destroyed as soon as we go out of its scope(which is function;s scope), so “temp” cant be accessed outside