C expert: Too Much Default Visibility
Whenever you define a C function, its name is globally visible by default. You can prefix the function
name with the redundant extern keyword or leave it off, and the effect is the same. The function is
visible to anything that links with that object file. If you want to restrict access to the function, you are
obliged to specify the static keyword.
function apple (){ /* visible everywhere */ }
extern function pear () { /* visible everywhere */ }
static function turnip(){ /* not visible outside this file */ }
In practice, almost everyone tends to define functions without adding extra storage-class specifiers, so
global scope prevails.
Whenever you define a C function, its name is globally visible by default. You can prefix the function
name with the redundant extern keyword or leave it off, and the effect is the same. The function is
visible to anything that links with that object file. If you want to restrict access to the function, you are
obliged to specify the static keyword.
function apple (){ /* visible everywhere */ }
extern function pear () { /* visible everywhere */ }
static function turnip(){ /* not visible outside this file */ }
In practice, almost everyone tends to define functions without adding extra storage-class specifiers, so
global scope prevails.
C语言函数可见性
本文介绍了C语言中函数默认的全局可见性,并解释了如何使用extern和static关键字来控制函数的可见范围。默认情况下,不加任何限定符的函数在整个链接过程中都是可见的。
2339

被折叠的 条评论
为什么被折叠?



