我不明白两者的意义。
当前回答
在这里找到类似的答案:C语言的技术面试问题。
声明为程序提供了一个名称;定义提供程序中实体(例如类型、实例和函数)的唯一描述。声明可以在给定的作用域中重复,它在给定的作用域中引入一个名称。
声明是定义,除非:
声明声明了一个函数,但没有指定函数体, 声明包含一个extern说明符,没有初始化式或函数体, 声明是没有类定义的静态类数据成员的声明, 声明是类名定义,
定义是声明,除非:
定义定义一个静态类数据成员, Definition定义一个非内联成员函数。
其他回答
宣言
声明告诉编译器a 程序元素或名称存在。一个 声明引入一个或多个 命名到程序中。声明可以 在一个程序中出现一次以上。 因此,类,结构, 枚举类型等 用户定义的类型可以声明为 每个编译单元。
定义
定义指定哪些代码或数据 名称描述。名称必须为 在使用之前声明。
定义意味着实际编写的函数,声明意味着简单的声明函数 如。
void myfunction(); //this is simple declaration
and
void myfunction()
{
some statement;
}
这是函数myfunction的定义
声明是在未分配值或对象的情况下创建原语或对象引用变量或方法。 int; Final int a;
定义意味着分别为值或对象赋值 Int a =10;
初始化意味着为各个变量或对象分配内存。
《K&R》(第二版)中有一些非常明确的定义;这有助于把它们放在一个地方,并作为一个整体来阅读:
"Definition" refers to the place where the variable is created or assigned storage; "declaration" refers to the places where the nature of the variable is stated but no storage is allocated. [p. 33] ... It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside. If the lines int sp; double val[MAXVAL] appear outside of any function, they define the external variables sp and val, cause storage to be set aside, and also serve as the declaration for the rest of that source file. On the other hand, the lines extern int sp; extern double val[]; declare for the rest of the source file that sp is an int and that val is a double array (whose size is determined elsewhere), but they do not create the variables or reserve storage for them. There must be only one definition of an external variable among all the files that make up the source program. ... Array sizes must be specified with the definition, but are optional with an extern declaration. [pp. 80-81] ... Declarations specify the interpretation given to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations that reserve storage are called definitions. [p. 210]
为了理解声明和定义之间的区别,我们需要查看程序集代码:
uint8_t ui8 = 5; | movb $0x5,-0x45(%rbp)
int i = 5; | movl $0x5,-0x3c(%rbp)
uint32_t ui32 = 5; | movl $0x5,-0x38(%rbp)
uint64_t ui64 = 5; | movq $0x5,-0x10(%rbp)
double doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
这只是定义:
ui8 = 5; | movb $0x5,-0x45(%rbp)
i = 5; | movl $0x5,-0x3c(%rbp)
ui32 = 5; | movl $0x5,-0x38(%rbp)
ui64 = 5; | movq $0x5,-0x10(%rbp)
doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
正如你所看到的,没有任何变化。
声明与定义不同,因为声明提供的信息仅供编译器使用。例如uint8_t告诉编译器使用asm函数movb。
看到:
uint def; | no instructions
printf("some stuff..."); | [...] callq 0x400450 <printf@plt>
def=5; | movb $0x5,-0x45(%rbp)
声明没有等价的指令,因为它不是要执行的东西。
此外,声明告诉编译器变量的作用域。
我们可以说,声明是编译器用来确定正确使用变量的信息,以及某些内存属于某个变量多长时间。
推荐文章
- 编程中的术语“上下文”?
- 通过SSH使用Eclipse完成远程项目
- int的最大值
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- C“int”的大小是2字节还是4字节?
- 多维数组如何在内存中格式化?
- printf()和puts()在C语言中的区别是什么?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 有效,但毫无价值的语法在开关情况下?
- 有一个好的Valgrind Windows的替代品吗?
- gcc在哪里查找C和c++头文件?
- c++中size_t和int的区别是什么?
- 在C和c++中静态变量存储在哪里?