将函数声明为 extern
Declaring a function as extern
这样声明一个函数(bar)有什么区别吗:
char *foo(char *pch)
{
extern char *bar(); /* this line here */
...
}
还是这样?
char *foo(char *pch)
{
char *bar(); /* this line here */
...
}
2011 C 标准在 6.2.2/5 中说:
If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern
.
所以没有技术差异。
但是正如评论中已经指出的那样,两者都被认为是糟糕的风格。函数声明不属于将使用它的另一个函数。如果您使用该模式并想要更改函数的声明,则需要查找并修改所有使用它的地方!具有外部链接的函数应该在头文件中声明。具有内部链接(使用 static
关键字)的函数应在源文件开头附近的某个位置声明。
这样声明一个函数(bar)有什么区别吗:
char *foo(char *pch)
{
extern char *bar(); /* this line here */
...
}
还是这样?
char *foo(char *pch)
{
char *bar(); /* this line here */
...
}
2011 C 标准在 6.2.2/5 中说:
If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier
extern
.
所以没有技术差异。
但是正如评论中已经指出的那样,两者都被认为是糟糕的风格。函数声明不属于将使用它的另一个函数。如果您使用该模式并想要更改函数的声明,则需要查找并修改所有使用它的地方!具有外部链接的函数应该在头文件中声明。具有内部链接(使用 static
关键字)的函数应在源文件开头附近的某个位置声明。