By
doesn't e get modified all times
I think you are asking why e
retains its original value even after it gets changed by recursive calls.
Part of the misunderstanding is the way e
is defined:
int e;
If you had instead said:
static int e;
then yes, every time the value gets changed in a recursive call, the value in the calling function will also get changed.
With static storage there is only a single instance of e
.
Defining it that way would indeed have produced incorrect results.
If you had instead said:
auto int e;
then no, the recursive calls will not affect the value in the calling function.
Each invocation of the function causes storage to be automatically allocated on the stack for another instance of e
.
If it recurses a thousand times, at the deepest level there will be a thousand distinct instances of e
on the stack.
And at each level, the function can see only its own copy.
Now C has a history of letting programmers be lazy (one of its major faults), and if one omits the word "auto" or "static" the compiler notices the context and decides what you really meant.
In this case, it did the right thing (it almost always does) and knew that you really meant "auto", and allocated a separate storage location for each invocation.
Most programmers do omit the keyword auto
, but I prefer to always use it, both to remind me that it isn't static, global, external, or (in older versions) block common, and to make it easier to find when I'm searching for definitions.