一个累了的问题? [英] A tired question?

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

问题描述

我在常见问题解答中看了这个,但仍然没有很好的理解它。


如果我已经定义了一个函数foo.c,如果为了

参数,foo.h包含该函数的声明,并且如果,作为

的答案之一类似的问题是所有定义也是

声明!然后需要使用#include foo.h。在foo.c?


提前致谢。

I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function''s declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?

Thanks in advance.

推荐答案

mdh写道:
mdh wrote:

我在常见问题解答中查看了这一点,但仍然没有很好的理解



如果我在foo.c中定义了一个函数,并且为了

参数,foo.h包含该函数的声明,以及如果,作为

类似问题的答案之一是所有定义也是

声明!然后需要使用#include foo.h。在foo.c?
I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function''s declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?



你通常可以在没有它的情况下离开,

但是侥幸逃脱不是很好的编码。


如果你#include foo.h"在foo.c,

然后编译器将检查函数类型是否匹配。


-

pete

You can usually get away without it,
but "getting away with something" isn''t good coding.

If you # include foo.h" in foo.c,
then the compiler will check that the function types match.

--
pete


文章< 72 ********************************* *@f1g2000prb.g ooglegroups.com>,

mdh< md ** @ comcast.netwrote:
In article <72**********************************@f1g2000prb.g ooglegroups.com>,
mdh <md**@comcast.netwrote:

>我看了在常见问题解答中,仍然没有很好的理解它。

如果我在foo.c中定义了一个函数,并且为了
参数有foo.h包含该函数的声明,如果,作为类似问题的答案之一是所有定义也是
声明!然后需要使用#include foo.h。在foo.c?
>I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function''s declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?



这并不是绝对必要的(因为函数的定义确实是
作为声明),但它'''这样做仍然是一个好主意,

,因为这样可以让编译器确保foo.h中的声明与boo.c中的声明匹配。 br />
编译器可以(并且在许多常见情况下需要)抱怨如果

它看到两个相同函数或变量的不兼容声明

in相同的翻译单位,但如果你不#include" foo.h"来自

foo.c,它不会同时看到两个版本[1],所以它不会...... / b $ b有机会这样做。


可诊断的错误总是比无声的未定义行为更好,

和无声的未定义行为是你最后得到的,如果你打电话给

被声明的函数(与调用代码不同)

被定义。

dave


[1对于一个足够聪明的编译器来说,仍然很可能安排这个进行检查,并且还有代码检查

工具(lint是规范的例子) )在编译器不太可能的地方特别寻找

不兼容的声明

来看它们,但它确实需要更多的努力。


-

爱斯基摩网上的Dave Vandervies dj3vande com


侮辱你的lusers,而不是你的同伴系统管理员的随机区域。

- 可怕的魔鬼修道院中的阿兰·J·罗森塔尔

It''s not strictly necessary (since the definition of the function does
serve as the declaration there), but it''s still a good idea to do so,
because that lets the compiler make sure that the declaration in foo.h
matches what''s in foo.c.
The compiler can (and in many common cases is required to) complain if
it sees two incompatible declarations of the same function or variable
in the same translation unit, but if you don''t #include "foo.h" from
foo.c, it won''t ever see both versions at the same time[1], so it won''t
have the opportunity to do that.

Diagnosable errors are ALWAYS better than silent undefined behavior,
and silent undefined behavior is what you end up getting if you call a
function that was declared (to the calling code) differently than it
was defined.
dave

[1] It''s still quite possible for a sufficiently clever compiler to
arrange for this to be checked, and there are also code checking
tools (lint is the canonical example) that specifically look for
incompatible declarations in places where the compiler is unlikely
to see them, but it does take rather more effort.

--
Dave Vandervies dj3vande at eskimo dot com

Insult your lusers, not random swaths of your fellow sysadmins.
--Alan J Rosenthal in the scary devil monastery


6月25日,7:51 * pm,dj3va ... @csclub.uwaterloo.ca.invalid写道:
On Jun 25, 7:51*pm, dj3va...@csclub.uwaterloo.ca.invalid wrote:

文章< 72164536-3dfd-4066-a4ec-46825b5cb ... @ f1g2000prb.googlegroups.com>,

In article <72164536-3dfd-4066-a4ec-46825b5cb...@f1g2000prb.googlegroups.com>,


如果我在foo.c中定义了一个函数,并且.......有foo.h包含那个* function'的声明,并且......全部定义也是

声明!然后需要使用#include foo.h。在foo.c?
If I have defined a function in foo.c, and .......have foo.h contain that *function''s declaration, and ...."All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?



这并不是绝对必要的(因为函数的定义是

作为声明),但它'''这样做仍然是一个好主意,

,因为这样可以让编译器确保foo.h中的声明与boo.c中的声明匹配。 br />
编译器可以(和

可诊断错误总是比无声未定义行为更好..........


It''s not strictly necessary (since the definition of the function does
serve as the declaration there), but it''s still a good idea to do so,
because that lets the compiler make sure that the declaration in foo.h
matches what''s in foo.c.
The compiler can (and in
Diagnosable errors are ALWAYS better than silent undefined behavior..........



不确定这是否正在歪曲哲学......这显然是一个很好的检查,几乎让编译器问你真的确定

函数定义是否正确写入?如果它可以根据声明检查

格式。除此之外......还有什么东西在

中用这种方式设置的C语言?(例如,关于

变量,K& R说(页40)所有变量必须是

在使用前声明,...)。我有在K& R中找不到任何语言

对函数做出如此具体的陈述。对不起,这听起来很尴尬,如果是这样的话,那就是我没有得到它。


Not sure if this is straying to the philosophical.... It is clearly a
good check, almost having the compiler ask "Are you really sure that
the function definition is correctly written?" if it can check the
format against a declaration. Other than that...is there something in
the C language that sets it up this way? ( For instance, as far as
variables are concerned, K&R say (Page 40) "All variables must be
declared before use,...). I have not found any language in K&R that
make such a specific statement about functions. Sorry is this sounds a
little dumb, if so, it''s me not getting it.


这篇关于一个累了的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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