问题描述
我的问题是关于术语声明的用法和含义。和
定义因为它与C语言有关。
我在阅读有关这样的事情时,我已经阅读了两者混合的消息来源
为一个存储设置存储空间对象,定义/声明一个结构,部分
函数,引用另一个模块中的外部变量。
sourcefile1.c
==============
extern long globalfoo; /* 宣言? * /
int main()
{
浮b; /* 宣言 ? * /
struct STRUCTFOO
{
int foobar;
}; / *定义? * /
struct STRUCTFOO foostruct; /* 宣言? * /
返回0;
}
sourcefile2.c
= ============
long globalfoo; / *定义实例?? Steve Summit教程称之为
这个* /
-
我使用的简单规则是,如果它放在一边存储它是'b $ b声明,否则它是一个定义。
即使有这个规则,功能似乎也不同。我们称之为
原型声明和标题/正文定义(即使这是'/ b $ b'它是标题/正文放置代码空间)。
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.
I''ve read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.
sourcefile1.c
==============
extern long globalfoo; /* declaration? */
int main()
{
float b; /* declaration ? */
struct STRUCTFOO
{
int foobar;
}; /* definition ? */
struct STRUCTFOO foostruct; /* declaration? */
return 0;
}
sourcefile2.c
=============
long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
--
The simple rule I use is that, if it sets aside storage it''s a
declaration, otherwise it''s a definition.
Even with this rule, functions seem to be different. We call
prototypes declarations, and headers/bodies definitions (even though
it''s the header/body that sets aside code space).
推荐答案
它是{和}以及它们之间的代码,组合
带有''函数头''(名称和参数列表),
,它们包含一个函数定义。
-Mike
It''s the { and } and the code between them, combined
with the ''function header'' (name and argument list),
which comprise a function definition.
-Mike
是的。也是一个定义。
Yes. Also a definition.
不,这是一个定义。
No, its a definition.
不,这是一个声明。
No, Its a declaration.
不,这是一个定义。
No, its a definition.
否,相反。
"定义"指的是创建或分配变量的地方
存储; "宣言"指变量的性质为
的地方,但没有分配存储空间。 -K& R II Page 33
所以,我们可以说定义也是一个宣言。我是对的吗?
是的。除了结构定义,它是一个''类型定义'',
并没有分配存储(但结构定义可以组合
与该结构类型的对象的定义:
结构x
{
int a;
} s;
定义类型''struct x''以及它的对象类型。
No, The Opposite.
"Definition" refer to the place where the variable is created or assigned
storage; "Declaration" refers to places where the nature of the variable is
stated but no storage is allocated." -K&R II Page 33
So, We can say Definition is also a Declaration. Am I right?
Yes. Except for a struct definition, which is a ''type definition'',
and does not allot storage (but a struct definition can be combined
with a definition of an object of that struct type:
struct x
{
int a;
} s;
defines type ''struct x'' as well as an object of that type.
你的意思是什么???
What do U mean by this???
是的,但声明不一定是原型。
Yes, but a declaration need not be a prototype.
它是{和}以及它们之间的代码,与''函数结合使用头''(名称和参数列表),
包含一个函数定义。
It''s the { and } and the code between them, combined
with the ''function header'' (name and argument list),
which comprise a function definition.
-Mike
- Neo
-Neo
是的。也是一个定义。
Yes. Also a definition.
不,这是一个定义。
No, its a definition.
不,这是一个声明。
No, Its a declaration.
不,这是一个定义。
No, its a definition.
否,相反。
"定义"指的是创建或分配变量的地方
存储; "宣言"指变量的性质为
的地方,但没有分配存储空间。 -K& R II Page 33
所以,我们可以说定义也是一个宣言。我是对的吗?
是的。除了结构定义,它是一个''类型定义'',
并没有分配存储(但结构定义可以组合
与该结构类型的对象的定义:
结构x
{
int a;
} s;
定义类型''struct x''以及它的对象类型。
No, The Opposite.
"Definition" refer to the place where the variable is created or assigned
storage; "Declaration" refers to places where the nature of the variable is
stated but no storage is allocated." -K&R II Page 33
So, We can say Definition is also a Declaration. Am I right?
Yes. Except for a struct definition, which is a ''type definition'',
and does not allot storage (but a struct definition can be combined
with a definition of an object of that struct type:
struct x
{
int a;
} s;
defines type ''struct x'' as well as an object of that type.
你的意思是什么???
What do U mean by this???
是的,但声明不一定是原型。
Yes, but a declaration need not be a prototype.
它是{和}以及它们之间的代码,与''函数结合使用头''(名称和参数列表),
包含一个函数定义。
It''s the { and } and the code between them, combined
with the ''function header'' (name and argument list),
which comprise a function definition.
-Mike
- Neo
-Neo
这篇关于"宣言"与“定义”相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!