Search code examples
crecursionstaticinitializationstorage-duration

how does a static variable not get reassigned when inside the function


I have a question regarding the answer in this question but I can not comment on it because I have less than 50 rep.

I was wondering in the answer foo() is being called multiple times and the static variable is being assigned the same number of times. So why is it that the static variable is not reassigned to 10 each time?


Solution

  • Actually static variables can get reassigned. But can't be redefined.

    Once a static variable defined it can't get redefined throughout the life time of program. But we can change the value.