Recently I came across a small issue, which bothered me for quite a while but came across some explanation to it. Though it still is a bit confusing.
char *getString()
{
char *str = "Print Me!";
return str;
}
int main()
{
printf("%s", getString());
getchar();
return 0;
}
Gives output as :
Print Me!
Although
char *getString()
{
char str[] = "Print Me!";
return str;
}
int main()
{
printf("%s", getString());
getchar();
}
Gives a garbage value. Why is it so.