寻求原型制定规则/建议 [英] Prototyping rules/recommendations sought

查看:64
本文介绍了寻求原型制定规则/建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我对函数的proptypes有疑问。什么是

推荐的做法?我这样做的方法是将所有外部函数

放在头文件中,同时在源文件的

起始处对内部(文件范围)函数进行原型设计。我见过很多人(特别是在linux下使用gcc

)没有做内部函数的原型设计,只是声明

他们内联可以这么说。


这种方法有什么建议优势/劣势吗?它好像b $ b似乎是在这里有一个权衡:


1.如果原型通过捕获不正确的参数来帮助我

类型,数字等..它们值得拥有


2.但是维护它们很麻烦。


所以,问题是,大多数现代编译器是否能够捕获类型/数字

错误,以便我可以避免内部函数的原型设计?你有什么建议吗?

Dear All,
I have a question regarding proptypes for functions. What is the
recommended practice? The way I do it is to put all my external functions
in a header file, while protyping internal (file scope) functions at the
start of the source file. I''ve seen many people (especially using gcc
under linux) don''t botehr prototyping internal functions but just declare
them inline so to speak.

Is there any recommendations advantages/disdvantages of the approach? It
seems to be that there is a tradeoff here:

1. If the prototypes are going to help me by catching incorrect parameter
types,numbers etc.. they are worth having

2. However its a hassle to maintain them.

So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?

推荐答案

rs写道:
rs wrote:
所以,问题是,大多数现代编译器能够捕获类型/数字错误,以便我可以避免内部函数的原型设计吗?你有什么建议?
So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?




如果一个函数没有声明,只有一个定义,并且在

之前使用它是定义后,它将在没有原型的情况下使用。如果预期的原型错误,某些编译器可能会发出警告,但他们可能不会从后面的定义中推断原型。所以你应该原型

这样的静态函数。其他静态函数不需要声明。


当然,你可能会忘记在使用静态函数之前

声明它,除非编译器警告隐式声明

函数(例如使用gcc -Wimplicit选项),所以你可能更喜欢

无论如何声明所有静态函数。


-

Hallvard



If a function has no declaration, only a definition, and is used before
it is defined, it gets used without a prototype. Some compilers may
give a warning if the expected prototype is wrong, but they may not
infer the prototype from the later definition. So you should prototype
such static functions. Other static functions need no declarations.

Of course, you might forget that you are using a static function before
declaring it unless the compiler warns about implicitly declared
functions (e.g. with the gcc -Wimplicit option), so you may prefer to
declare all static functions anyway.

--
Hallvard




" rs" <无************ @ talk21.com> schrieb im Newsbeitrag

新闻:bq ********** @ pegasus.csx.cam.ac.uk ...

"rs" <no************@talk21.com> schrieb im Newsbeitrag
news:bq**********@pegasus.csx.cam.ac.uk...
亲爱的所有人,

我对函数的proptypes有疑问。什么是推荐的做法?我这样做的方法是将所有外部函数放在头文件中,同时在源文件的原始文件中对内部(文件范围)函数进行原型设计。我见过很多人(特别是在linux下使用gcc
)没有做内部函数的原型设计,但只是声明它们内联可以说。

有没有建议优势/优势的方法?它似乎是在这里有一个权衡:

1.如果原型将通过捕获不正确的参数
类型,数字等来帮助我..它们是值得的

2.然而,维护它们很麻烦。

所以,问题是,大多数现代编译器能够捕获类型/数字错误,以便我可以避免内部函数的原型设计?您有什么建议?
Dear All,
I have a question regarding proptypes for functions. What is the
recommended practice? The way I do it is to put all my external functions
in a header file, while protyping internal (file scope) functions at the
start of the source file. I''ve seen many people (especially using gcc
under linux) don''t botehr prototyping internal functions but just declare
them inline so to speak.

Is there any recommendations advantages/disdvantages of the approach? It
seems to be that there is a tradeoff here:

1. If the prototypes are going to help me by catching incorrect parameter
types,numbers etc.. they are worth having

2. However its a hassle to maintain them.

So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?




不是建议(我是谁给出建议)..

我写的功能在编译单元中,颠倒并将原型类型只用那些从不同编译单元调用的函数


到一个标题中:

某些代码中的
。 c:


int foo(无效)

{

返回1;

}


双栏(double some_val)

{

返回some_val / 1.234;

}


int main(无效)

{

int int_val = foo();

double my_dbl = bar (2.55);

返回0;

}

somecode.hi中的
只有


双栏(双);

因为foo()仅在somecode.c中使用,但是bar()也从

someothercode.c中调用。


只是我的<你喜欢的任何货币> 0.02

Robert



Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put protoypes
of only those functions which are called from a different compilation unit
into a header:

in somecode.c:

int foo(void)
{
return 1;
}

double bar(double some_val)
{
return some_val / 1.234;
}

int main(void)
{
int int_val = foo();
double my_dbl = bar(2.55);
return 0;
}

in somecode.h i have only

double bar(double);
because foo() is used only in somecode.c, but bar() is called from
someothercode.c as well.

just my <whatever currency you like> 0.02
Robert


" Robert Stankowic" < PC ****** @ netway.at>写道:
"Robert Stankowic" <pc******@netway.at> wrote:
" rs" <无************ @ talk21.com> schrieb:
< snip>
"rs" <no************@talk21.com> schrieb: <snip>
所以,问题是,大多数现代编译器能够捕获类型/数字错误,以便我可以避免内部函数的原型设计?您的建议是什么?
So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?



不是建议(我是谁给出建议)..
我在编译单元中编写函数颠倒 ;并将只有那些从不同编译单元调用的函数的原型转换成标题:

在somecode.c中:

int foo (无效)
{
返回1;
}



Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put protoypes
of only those functions which are called from a different compilation unit
into a header:

in somecode.c:

int foo(void)
{
return 1;
}




您可能希望将其更改为:


static int foo(无效)

...


< snip>只是我的<你喜欢的任何货币> 0.02



You may want to change this to:

static int foo(void)
...

<snip> just my <whatever currency you like> 0.02




我添加了我的,这是0.04。 ;-)


问候

-

Irrwahn

(ir ***** **@freenet.de)



I added mine, that makes 0.04. ;-)

Regards
--
Irrwahn
(ir*******@freenet.de)


这篇关于寻求原型制定规则/建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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