在“主要”之前? [英] Ahead of "main"?

查看:47
本文介绍了在“主要”之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

通过K& amp; R(你们中的一些人可以证明这是
!),我从来没有看到在main中声明一个函数

的一个很大的差异。或前方它的。现在,(p119,K& R II),讨论

陈述功能无论什么 "应该在

main之前宣布。

有充分的理由吗?

谢谢。

Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a big diffference in declaring a function
within "main" or "ahead" of it. Now, (p119, K&R II), the discussion
states that "functions "whatever" " should be declared ahead of
main.
Is there a good reason for this?
thanks.

推荐答案



" mdh" < md ** @ comcast.netwrote in message

news:11 ********************* @ h2g2000hsg.googlegrou ps.com ...

"mdh" <md**@comcast.netwrote in message
news:11*********************@h2g2000hsg.googlegrou ps.com...

大家好,

通过K& K进行有条不紊的行动R(你们中的一些人可以证明这是
!),我从来没有看到在main中声明一个函数

的一个很大的差异。或前方它的。现在,(p119,K& R II),讨论

陈述功能无论什么 "应该在

main之前宣布。

有充分的理由吗?

谢谢。
Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a big diffference in declaring a function
within "main" or "ahead" of it. Now, (p119, K&R II), the discussion
states that "functions "whatever" " should be declared ahead of
main.
Is there a good reason for this?
thanks.



C的某些早期版本具有本地函数,在调用它们的函数

中声明。这个想法从未流行起来,现在不可能在$ main中声明
声明函数。

旧C也没有原型。因此,如果您按照

层次结构的相反顺序放置函数,编译器可以对参数进行额外的检查。现在

我们应该对所有函数进行原型化,所以放置main()的位置并不重要,尽管它应该是第一个或最后一个函数

的可读性。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

Some early versions of C had local functions, declared within the function
that called them. The idea never caught on, and it is now not possible to
declare functions within main.
Old C also had no prototypes. So if you put functions in reverse order of
hierarchy, the compiler could do additional checking of arguments. Nowadays
we should prototype all functions, so it doesn''t matter where main() is
placed, though obviously it should be either the first or the last function
for readbility.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


mdh写道:
mdh wrote:

大家好,

非常有条不紊地通过K& R(你们中的一些人可以证明这是
!),我从来没有看到在main中声明一个函数

的一个很大的差异。或前方它的。现在,(p119,K& R II),讨论

陈述功能无论什么 "应该在

main之前宣布。

这有充分的理由吗?
Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a big diffference in declaring a function
within "main" or "ahead" of it. Now, (p119, K&R II), the discussion
states that "functions "whatever" " should be declared ahead of
main.
Is there a good reason for this?



是的。如果在声明它之前使用函数,

编译器会对函数

所采用的参数及其返回的值进行假设。如果这些假设不符合函数的实际功能,那你就麻烦了。

(最新的编译器试图让你摆脱麻烦<没有做出任何假设;相反,他们会发出错误消息。)


这里有一个值得思考的问题:功能定义 -

类型,名称,参数和函数体包含在{} -

不仅定义了函数,还声明了它(两个

双音节动词都以DE开头,但它们不是相同的b $ b。可以通过编写所有相同的东西但省略

函数体并放置一个函数来声明一个没有

定义它的函数。那些{} - 封闭的东西

本来就是:


双重麻烦(int fireBurn,int cauldronBubble);


这告诉编译器有关参数trouble()期望

及其返回的值的类型,这足以允许编译器正确调用它(
)并检查一些

错误,比如只写一个参数)。在这样的

申报行之后的任何时候,你可以调用这个功能,并且

一切都会好的。


还是晚些时候,在文件的某个方便的位置,你可以提供函数的实际定义:


双重麻烦(int fireBurn,int cauldronBubble)

{

if(fireBurn!= 0)

return(double)cauldronBubble / fireBurn;

else

返回42.0;

}


有两个主要原因(以及一些不太紧迫的原因)

因为缺乏写出仅限声明的行。首先,当你转移到更大的程序时,你会发现自己打破了它们

到单独编译的文件中:为什么要复制

整个麻烦()进入二十个不同的程序,当

你可以在一个文件中编译一次然后让所有二十个
程序调用它吗?为了使这项工作,您需要写一个

仅用于声明的行(),其他二十个
程序可以使用;通常的做法是将这一行放在

a .h文件中,二十个程序都可以#include。


第二个原因弹出频率较低,但是偶尔发生

。如果你有两个函数macduff()和

macbeth()怎么办?在某些情况下,每个函数调用

另一个?


double macduff(int x){

...

if(!i_am_thane)

y = macbeth(x);

...

}


double macbeth(int y){

...

layOn = macduff(y + 42);

...

}


无论哪个你首先定义的定义,当你进入

调用时,其他函数的'

定义将不会出现。编译器会对

这个尚未定义的函数做出错误的假设,或者会在

角落中抗议和生气。解决方案是为

一个(或两个)函数编写一个声明专用行:


double macbeth(int); / *可选:省略arg名称* /


双macduff(int x){

...

}


double macbeth(int y){

...

}


- -

Eric Sosman
es ***** @ acm-dot-org .inva


4月29日下午1:41,Eric Sosman< esos ... @ acm-dot-org.invalidwrote:
On Apr 29, 1:41 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:

mdh写道:
mdh wrote:

大家好,

非常有条不紊地通过K& R(因为你们中的一些人可以证明

来!),我从来没有见过...在宣称一个函数时的差异

在main中或前方它的。
Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a ... diffference in declaring a function
within "main" or "ahead" of it.


>

是的。如果你在声明它之前使用了一个函数,.....
>
Yes. If you use a function before declaring it,.....


这里是一个值得思考的问题:一个函数定义--... ..也宣布它(两个

双音节动词,都以DE开头,但它们不是相同的
)。可以声明一个没有

定义它的函数,......
Here''s a point to ponder: A function definition --..... also declares it (two
two-syllable verbs both beginning with DE, but they are not
the same). It is possible to declare a function without
defining it,......



谢谢....就像我一样C越来越多,我意识到这个词确实很有价值。


Thank you....as I do C more and more, I realize that words really
count.



>

有两个主要原因(以及一些不太紧迫的原因)

想要编写仅限声明的行。首先,当你转移到更大的程序时,你会发现自己打破了它们。分成编译的文件:
>
There are two main reasons (and some less pressing ones)
for wanting to write declaration-only lines. First, as you
move to larger programs you''ll find yourself breaking them
up into separately-compiled files:



>

第二个原因弹出频率较低,但确实偶尔发生
。如果你有两个函数macduff()和

macbeth(),在某些情况下每个函数调用

另一个怎么办?

>
A second reason pops up less frequently, but does occur
now and then. What if you have two functions macduff() and
macbeth(), and under some circumstances each of them calls
the other?




谢谢Eric。

所以,如果我理解正确的话,就我的小程序而言,

key似乎是要理解声明需要在被调用的函数之前发生。

该声明实际发生的地方(在main之前,或在main之前) )

(在这些有限的情况下)无关紧要。但是,一旦进入真实状态,

C世界,这将是很好的

编程(正如你所说的单独.h文件),所以我不妨

习惯练习好风格!!



Thank you Eric.
So, if I understand you correctly, in my case for a small program, the
key seems to be to understand that a declaration needs to occur prior
to the function being called.
Where that declaration actually occurs (prior to main, or within main)
( under these limited circumstances) is of no consequence. However,
once one gets into the "real" world of C, this would be good
programming ( as you noted with seperate .h files), so I may as well
get used to practising good style!!


这篇关于在“主要”之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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