Static functions and variables in C

2019-09-18 09:51发布

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?

2条回答
乱世女痞
2楼-- · 2019-09-18 10:35

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.

查看更多
再贱就再见
3楼-- · 2019-09-18 10:56

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).

查看更多
登录 后发表回答