static function in C

前端 未结 7 1585
天涯浪人
天涯浪人 2020-11-30 17:11

What is the point of making a function static in C?

7条回答
  •  温柔的废话
    2020-11-30 17:35

    pmg is spot on about encapsulation; beyond hiding the function from other translation units (or rather, because of it), making functions static can also confer performance benefits in the presence of compiler optimizations.

    Because a static function cannot be called from anywhere outside of the current translation unit (unless the code takes a pointer to its address), the compiler controls all the call points into it.

    This means that it is free to use a non-standard ABI, inline it entirely, or perform any number of other optimizations that might not be possible for a function with external linkage.

提交回复
热议问题