In said:
You are invoking a undefined behavior. Auto variables have
scope/life only until the function in which they are defined returns.
After which their value is junk.
So what? The return statement is executed while the auto objects are
still alive, not *after*.
So in your case .. anything can happen !!
Bullshit! Or, more precisely, anything can happen because of the bad
printf call, which is the *real* cause of the OP's problem.
print the value (sum*sum) inside the number function. You get what you
expect.
Nope, if he uses the same printf call, he will get the same garbage.
You're a marvelous illustration of the saying "a little knowledge is a
dangerous thing". Don't use *any* rule you don't *really* understand.
The rule in question is about not returning the *address* of automatic
objects, because the objects no longer exist after the function returns.
However, it is always safe to return their values, because the value is
passed back to the caller using a special protocol that doesn't require
the existence of the object when the caller is getting back its value.
But, in this example, even this is irrelevant, as the function doesn't
return sum, but sum * sum. You don't expect this expression to be
evaluated by the caller, do you? So, the function didn't return the
value of *any* automatic object, it returned the value of an arbitrary
expression, value that need not be stored in any memory location.
Mechanically memorised rules are doing you no good. If you cannot
understand something, either ignore it or ask for better explanations.
Dan