编译器设计中的定义和声明有什么区别?

定义

它识别与变量、函数、类等名称相关的代码或数据。编译器本质上需要定义来为声明的实体指定存储区域。当一个变量被定义时,它有一个包含该变量的多个字节的内存量。

函数定义为函数生成代码。它只能在一个程序中定义一个程序元素一次,因为该定义是一个程序元素的唯一要求。声明和定义之间的关系可以是一对多的。

宣言

它可以确定程序的名称,包括变量、函数、命名空间、类等的名称。没有声明就不能在程序中使用名称。

程序元素可以使用不同的定义多次声明。仅当使用精确格式形成多个声明时,才能生成各种声明。从编译器的角度来看,声明是支持程序元素的可感知性的媒介。

让我们看看定义和声明之间的比较。

定义声明
The definition specifies attributes and causes storage allocation.
该声明指定类型和不同的属性,但不生成存储分布。
The data included in the definition is only used during translation. The language translator enters the data from the type definition into the table during translation and whenever the type name is referenced in a successive declaration, used the tabled data to create the suitable executable program for setting up and manipulating the desired information objects during execution.
变量声明中包含的数据主要在翻译过程中用于决定信息对象的存储表示以及存储管理和类型检查目标。
For a particular name, a C program can have only a single definition.
对于特定名称,C 程序可以有各种兼容的声明。
The type definition enables some elements of translation including deciding storage representations to be completed only once for a single type definition.
类型声明多次确定不同声明的存储表示。
The inclusion of type definitions in a language does not generally modify the runtime organization of the language execution.
在语言中包含类型定义可能会改变语言的运行时组织,因为它们用于设置运行时数据对象。