I know that, if we declare variables inside a function without allocating memory for them, they will be lost after the function finishes its job.
The following code prints:
(null)
5
char* getString()
{
char arr[] = "SomeText";
return arr;
}
int getInt()
{
int b = 5;
return b;
}
int main()
{
printf("%s", getString());
printf("\n");
printf("%d", getInt());
return 0;
}
Both arr
and b
variables are created on the stack, so they both should be destroyed when the functions end. My question is, how come variable b
doesn't get lost while variable arr
is lost?