另一个函数中的 C 函数声明

发布于 2024-10-16 03:30:08 字数 121 浏览 6 评论 0原文

谁能向我解释这些行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹊巢 2024-10-23 03:30:08

是的,你的猜测是正确的。它声明了函数 abc() 的存在,因此可以在 xyz() 中引用它。请注意,extern 是不必要的,因为默认情况下函数是 extern

Yes, your guess is correct. It's declaring the existence of the function abc(), so it may be referenced within xyz(). Note that the extern is unnecessary, as functions are extern by default.

み格子的夏天 2024-10-23 03:30:08

这种声明方式有一个很大的优点:

如果只有一个或更少的函数调用外部函数,那么这种声明就有意义,尤其是在大型源文件中。
如果必须进行稍后的代码重组(将函数移动到另一个文件中),那么查看依赖关系比在全局(文件)范围上添加外部要容易得多。
在后一种情况下,“忘记”文件中的此类外部信息的可能性较高。相比之下,通过在函数作用域中声明它,声明将与函数一起移动......

我也倾向于对外部全局变量这样做 - 在维护和最终重组/最小化依赖关系时,好处会稍后出现。

关于“写外部/非外部”主题的最后一个注释:
如果它只是一个前向声明(->该函数是在同一文件的末尾定义的),我不建议使用外部 - 因为事实并非如此。
否则,外部绝对有意义,表明必须在其他地方找到定义(或者对于库:可能需要由该库的用户实现)。

希望这会有所帮助(作为迈向更客观的编程风格的一步..:))

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.. :) )

单挑你×的.吻 2024-10-23 03:30:08

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.

海拔太高太耀眼 2024-10-23 03:30:08

我只想补充一点,根据我的经验,这种构造在现代代码中并不常见,但在较旧的代码中很常见,尤其是“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.

心病无药医 2024-10-23 03:30:08

是的,您的陈述是正确的......当我们使用 extern func_name 时,我们正在声明 func_name。

Yes your statement is correct.....when we use extern func_name w are are declaring the func_name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文