静态函数原型 [英] Static function prototype

查看:82
本文介绍了静态函数原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎记得在ANSI C中,所有静态函数都应该具有

它们在文件开头列出的函数原型

更好的一致性(或类似的东西)那)。我用Google搜索,并没有发现

对此有任何好的解释。请问专家给我一个

的解释或者给我一些链接?谢谢!


/为什么茶

I seem to remember that in ANSI C, all static functions should have
their function prototypes listed at the beginning of the file for
better consistency (or something like that). I googled and didn''t find
any good explanation about it. Could the experts please give me an
explanation or point me to some link? Thanks!

/Why Tea

推荐答案

10月3日下午2:08,为什么茶< ; ytl ... @ gmail.comwrote:
On Oct 3, 2:08 pm, Why Tea <ytl...@gmail.comwrote:

我似乎记得在ANSI C中,所有静态函数都应该具有

它们的函数在文件开头列出的原型

更好的一致性(或类似的东西)。我用Google搜索,并没有发现

对此有任何好的解释。请问专家给我一个

的解释或者给我一些链接?谢谢!


/为什么茶
I seem to remember that in ANSI C, all static functions should have
their function prototypes listed at the beginning of the file for
better consistency (or something like that). I googled and didn''t find
any good explanation about it. Could the experts please give me an
explanation or point me to some link? Thanks!

/Why Tea



在一个文件中,每个函数只能看到它上面声明的函数。

这就是为什么将所有原型放在

文件顶部的好习惯。在你调用

之前尚未声明的函数将被假设为返回int并且据我所知,由于没有定义,因此不会检查
的参数检查他们

反对。这适用于所有静态或非静态函数。

In a file each function can only see the ones declared above it.
That''s why it''s good practice to have all the prototypes at the top of
the file. The functions that have not been declared before you invoke
them will be asumed to return int and as far as I know the arguments
will not be checked because there is no definition to check them
against to. This applies to all functions static or not.


为什么Tea写道:
Why Tea wrote:

我似乎记得在ANSI C,所有静态函数都应该具有

它们在文件开头列出的函数原型,以便

更好的一致性(或类似的东西)。我用Google搜索,并没有发现

对此有任何好的解释。请问专家给我一个

的解释或者给我一些链接?谢谢!
I seem to remember that in ANSI C, all static functions should have
their function prototypes listed at the beginning of the file for
better consistency (or something like that). I googled and didn''t find
any good explanation about it. Could the experts please give me an
explanation or point me to some link? Thanks!



稍微退一步,在使用它们之前,

声明函数(静态或其他)总是好的做法。 (C99

使这不仅仅是一个好的做法,而是一个要求。)自从ANSI C出现以来,一直是好的做法,这是使用原型的好习惯。 >函数声明和定义中的
。所以关于

声明函数的建议适用于所有函数,而不仅仅是静态

函数。


现在布局问题,这是你真正的问题

归结为。 严重中的非静态功能程序应该通过包含相关的头文件来声明
,所以你在.c源文件中看到的所有
通常都是一些#include指令。

几乎永远不会在标题中声明静态函数:你

使它们变为静态,因为你希望它们的名字只在一个模块中可见

,那你为什么要这样做?将这些名称导出到其他

模块?因此,静态函数通常以不同的方式声明外部函数 - 声明本身看起来与静态相同,但它们的位置不同。 br />

模块中安排函数的两种主要风格

是常见的。其中一个放置顶级函数 - 例如main(),

- 在文件的早期,稍后使用从属函数

和低级leaf功能最后。在这种风格中,

函数的定义通常出现在至少一次

调用之后,也许是在所有这些调用之后,所以很好的做法(或C99的'

强制要求)要求在第一次使用之前在某个地方单独申报

。文件的顶部是一个非常好的位置。


有些人(我是其中之一)不喜欢告诉编译器

两次相同的事情;它让我们觉得foo()接受两个int

参数并返回一个double,然后右转并且

用两个写foo()的实际定义int参数和

double结果。当我们受到足够的厌恶时,我们有时会利用

这个函数的定义也可以作为它的

声明:如果定义出现在函数之前'第一次使用
,不需要单独声明。所以我们写模块

颠倒或Pascal-style:低水平的叶子函数在

顶部,其次是中级函数,最后是Big Kahuna

,每个函数都按照自己的定义声明。


如果你有两个或更多的相互递归函数 - f()

调用g()*和* g()调用f() - 你赢了'所有人都可以使用

定义声明策略。如果f()

首先出现,它对g()的调用出现在g()

的定义之前,你需要插入一个单独的g声明()某处

之前文件中的f()。


-
Er ********* @sun.com


10月3日,11:59 * am,Eri​​c Sosman< Eric.Sos ... @ sun.comwrote:
On Oct 3, 11:59*am, Eric Sosman <Eric.Sos...@sun.comwrote:

为什么Tea写道:
Why Tea wrote:

我似乎记得在ANSI C中,所有静态函数应该具有

它们在文件开头列出的函数原型,以便

更好的一致性(或者某些东西)像那样)。我用Google搜索,并没有发现

对此有任何好的解释。请问专家给我一个

的解释或者给我一些链接?谢谢!
I seem to remember that in ANSI C, all static functions should have
their function prototypes listed at the beginning of the file for
better consistency (or something like that). I googled and didn''t find
any good explanation about it. Could the experts please give me an
explanation or point me to some link? Thanks!



* * *退一点,

在使用之前声明函数,静态或其他方式一直都是好习惯他们。 *(C99

使这不仅仅是一种良好的做法,而是一项要求。)*并且自从ANSI C出现以来一直是b $ b,使用原型是一种很好的做法br />
函数声明和定义。 *所以关于

声明函数的建议适用于所有函数,而不仅仅是静态

函数。


* * *现在关于布局问题,这就是你的问题,实际上是b $ b b归结为。 *严重的非静态功能程序应该通过包含相关的头文件来声明
,所以你在.c源文件中看到的所有
通常都是一些#include指令。

几乎永远不会在标题中声明静态函数:你

使它们变为静态,因为你希望它们的名字只在一个模块中可见

,那你为什么要这样做?将这些名称导出到其他

模块? *因此,静态函数通常与外部函数声明不同

- 除了静态之外,声明本身看起来是相同的,但它们的位置是不同的。


* * *在模块中安排函数的两种主要风格

是常见的。 *一个放置顶级函数 - 例如main(),

- 在文件的早期,稍后使用从属函数

和低级leaf ;功能最后。 *在这种风格中,

函数的定义通常出现在至少一次

调用之后,也许是在所有这些调用之后,这样的好习惯(或C99'') s

强制要求)要求在第一次使用之前将某个单独的声明放在

之前。 *文件的顶部是一个非常好的位置。


* * *有些人(我是其中之一)不喜欢告诉编译器

同样的事情两次;它让我们觉得foo()接受两个int

参数并返回一个double,然后右转并且

用两个写foo()的实际定义int参数和

double结果。 *当我们受到足够的厌恶时,我们有时会利用

这个函数的定义也可以作为它的

声明:如果定义出现在函数的第一个

使用,不需要单独的声明。 *所以我们写模块

颠倒或Pascal-style:低水平的叶子函数在

顶部,其次是中级函数,最后是Big Kahuna

,每个函数都按照自己的定义声明。


* * *如果你有两个或更多的相互递归函数 - f()

调用g()*和* g()调用f() - 你不可能为所有这些使用

定义声明策略。 *如果首先出现f()

,它对g()的调用出现在g()

的定义之前,你需要插入一个单独的声明g()某个地方
文件中f()之前的



-

Eric.Sos ... @ sun .com


* * *Stepping back a little, it''s always been good practice to
declare functions, static or otherwise, before using them. *(C99
makes this not just good practice, but a requirement.) *And ever
since ANSI C appeared, it''s been good practice to use prototypes
in function declarations and definitions. *So the advice about
declaring functions applies to all functions, not just to static
functions.

* * *Now to matters of layout, which is what your question really
boils down to. *Non-static functions in "serious" programs should
usually be declared by including the associated headers, so all
you see in the .c source file is usually a few #include directives.
Static functions should almost never be declared in headers: You
made them static because you wanted their names to be visible only
inside one module, so why would you export those names to other
modules? *Thus, static functions are usually declared differently
from external functions -- the declarations themselves look the
same, apart from `static'', but their placement is different.

* * *Two principal styles of arranging functions within a module
are commonly seen. *One puts the top-level functions -- main(),
for example -- early in the file, with subordinate functions later
and low-level "leaf" functions last of all. *In this style the
function''s definition usually appears after at least one of its
calls, perhaps after all of them, so good practice (or C99''s
mandate) calls for putting a separate declaration somewhere prior
to the first use. *The top of the file is a pretty good location.

* * *Some people (I''m one of them) dislike telling the compiler the
same thing twice; it irks us to declare that foo() takes two int
arguments and returns a double, and then turn right around and
write the actual definition of foo() with two int arguments and a
double result. *When we get irked enough, we sometimes take advantage
of the fact that a function''s definition can also serve as its
declaration: If the definition appears before the function''s first
use, no separate declaration is needed. *So we write the module
"upside-down" or "Pascal-style:" low-level "leaf" functions at the
top, followed by intermediate-level functions, and the Big Kahuna
last of all, each function declared by its own definition.

* * *If you''ve got two or more mutually recursive functions -- f()
calls g() *and* g() calls f() -- you won''t be able to use the
definitions-are-declarations strategy for all of them. *If f()
appears first, its call to g() appears before the definition of g()
and you''ll need to insert a separate declaration of g() somewhere
before f() in the file.

--
Eric.Sos...@sun.com



感谢Eric提供了如此明确的解释。当我引起你的注意时,

我们可以看看extern吗?还有? :)如果我们已经导出所有导出的

函数和包含在头文件中的全局变量,那么

意味着实际上不需要使用extern(在模块中

需要访问这些函数和全局变量)因为我们

可以简单地#include头文件?有什么需求/优点

使用extern?


/为什么茶

Thanks Eric for such a clear explanation. While I get your attention,
can we take a look at "extern" as well? :) If we have all exported
functions and global variables included in a header file, does
it mean that there is really no need to use extern (in the module
that needs to access those functions and global variables) as we
can simply #include the header file? What are the needs/advantages
of using extern?

/Why Tea


这篇关于静态函数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆