Internal static variables in C, would you use them?

后端 未结 13 1441
甜味超标
甜味超标 2020-12-07 19:09

In C you can have external static variables that are viewable every where in the file, while internal static variables are only visible in the function but is persistent

13条回答
  •  有刺的猬
    2020-12-07 19:45

    Some use cases for static variables:

    • you can use it for counters and you won't pollute the global namespace.
    • you can protect variables using a function that gets the value as a pointer and returns the internal static. This whay you can control how the value is assigned. (use NULL when you just want to get the value)

提交回复
热议问题