本文介绍了是“ int a;” C和C ++中的声明或定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int a;
是C中的声明还是定义? C ++有什么区别吗?
Is int a;
a declaration or definition in C? Is there any difference in C++?
我一直以为这只是一个声明,直到今天...
I was always thinking it's just a declaration, until today...
是声明还是定义?
推荐答案
声明描述了一个对象,而 definition 请求创建对象。所有定义也是声明,但事实并非如此。
A declaration describes an object, whereas a definition requests the creation of an object. All definitions are also declarations, but the reverse is not true.
对于 int a;
,具体取决于它在源代码中的位置:
In the case of int a;
, what it is depends on where it is placed in the source code:
- 内部一个函数
int a;
是一个定义-它要求创建一个具有自动存储持续时间的int
变量(并且当然也是声明); - 在文件范围内,答案在C和C ++中是不同的。在C中,
int a;
是一个临时定义(只要类型和链接都可以接受,可以有多个定义)。 ;在C ++中,它是具有外部链接的普通定义。 - 在
结构
或工会说明符,
int a;
是成员的声明。
Within a function,
int a;
is a definition - it requests the creation of anint
variable with automatic storage duration (and of course also a declaration);At file scope, the answer is different in C and C++. In C,
int a;
is a tentative definition (of which there can be more than one, as long as the types and linkage are agreeable); in C++, it is an ordinary definition with external linkage.Within a
struct
orunion
specifier,int a;
is a declaration of a member.
这篇关于是“ int a;” C和C ++中的声明或定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!