[Solved] Variable declaration using static keyword [closed]


Assuming C/C++, I found this here:

(1) The use of static inside a function … means that once the variable has been initialized, it remains in memory until the end of the program. You can think of it as saying that the variable sticks around, maintaining its value, until the program completely ends. For instance, you can use a static variable to record the number of times a function has been called simply by including the lines static int count =0; and count++; inside the function.

(2) Because count is a static variable, the line “static int count = 0;” will only be executed once. Whenever the function is called, count will have the last value assigned to it.

So, two things going on: from (1), a will stick around for the whole program. From (2), the declaration/initialization of a will happen exactly once, at the beginning. Subsequent recursive calls on your function will not redeclare/reinitialize a.

10

solved Variable declaration using static keyword [closed]