另一个函数中的 C 函数声明
谁能向我解释这些行:
int xyz( void )
{
extern void abc( void );
}
函数定义中的函数声明? 或者我误解了什么?
can anyone explain these lines to me:
int xyz( void )
{
extern void abc( void );
}
a function declaration within a function definition?
or am I missunderstanding something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,你的猜测是正确的。它声明了函数
abc()
的存在,因此可以在xyz()
中引用它。请注意,extern
是不必要的,因为默认情况下函数是extern
。Yes, your guess is correct. It's declaring the existence of the function
abc()
, so it may be referenced withinxyz()
. Note that theextern
is unnecessary, as functions areextern
by default.这种声明方式有一个很大的优点:
如果只有一个或更少的函数调用外部函数,那么这种声明就有意义,尤其是在大型源文件中。
如果必须进行稍后的代码重组(将函数移动到另一个文件中),那么查看依赖关系比在全局(文件)范围上添加外部要容易得多。
在后一种情况下,“忘记”文件中的此类外部信息的可能性较高。相比之下,通过在函数作用域中声明它,声明将与函数一起移动......
我也倾向于对外部全局变量这样做 - 在维护和最终重组/最小化依赖关系时,好处会稍后出现。
关于“写外部/非外部”主题的最后一个注释:
如果它只是一个前向声明(->该函数是在同一文件的末尾定义的),我不建议使用外部 - 因为事实并非如此。
否则,外部绝对有意义,表明必须在其他地方找到定义(或者对于库:可能需要由该库的用户实现)。
希望这会有所帮助(作为迈向更客观的编程风格的一步..:))
This way of declaration has one big advantage:
If only one or less functions are calling an external function, this declaration makes sense especially within a big source file.
If a later code restructuring (function move in another file) has to be done, it is much easier to see the dependencies than to add externals on global (file) scope.
In the latter case the probability of "forgetting" such externals in a file is higher. In contrast by declaring it in function scope, the declaration will move together with the function...
I also tend to do so for external global variables - the benefit comes later when maintaining and eventually restructuring / minimizing dependencies.
One last note to the topic "writing external / not external":
If its just a forward declaration (-> the function is defined at end of the the same file), I would not recommend using external - because it simply isn't the case.
Otherwise external makes absolute sense to indicate that definition has to be found somewhere else (or for libaries: might need to be implemented by users of that libary).
Hope this helps (as a step to a more objective oriented programming style.. :) )
C 中的“extern”声明用于指示全局变量或函数的存在及其类型。
extern 是在当前模块外部定义的东西。
声明为 extern 的函数原型也并不罕见。
仅当它不是默认值和/或您想要指定“C”链接时才需要它。
The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function.
An extern is something that is defined externally to the current module.
It is also not uncommon to find function prototypes declared as extern.
You need it only where it's not the default, and/or where you want to specify "C" linkage.
我只想补充一点,根据我的经验,这种构造在现代代码中并不常见,但在较旧的代码中很常见,尤其是“K&R”C 代码。
更现代的代码通常会从头文件中获取函数原型。
I would just add that this construct, in my experience, is uncommon in modern code, but often seen in older code, especially "K&R" C code.
More modern code would usually get the function prototype from a header file.
是的,您的陈述是正确的......当我们使用 extern func_name 时,我们正在声明 func_name。
Yes your statement is correct.....when we use extern func_name w are are declaring the func_name.