且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是“ int a;” C和C ++中的声明或定义?

更新时间:2022-12-24 21:21:57

声明描述了一个对象,而 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 ++中,它是具有外部链接的普通定义。

  • 结构工会$中c $ c>说明符, int a; 是成员的声明。

  • Within a function, int a; is a definition - it requests the creation of an int 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 or union specifier, int a; is a declaration of a member.