Static functions and variables in C

2019-09-18 10:36发布

问题:

I know what is the purpose of using static variables in an object oriented language, still, I don't understand what is the meaning of using the "static" keyword in C. Can someone explain it to me?

回答1:

On a function or global variable, static makes the function or global variable local to that file; other files cannot access that function or global variable by that name (but they can access it if you give a pointer to it away).

On a local variable, it makes it act as if it was a global variable, but is only accessible within that function (unless, again, you give a pointer to it away).



回答2:

The value that a static variable has upon leaving a function is the same value that variable will have the next time the function is called.

A static function can be called only from within the same file that the function appears.