![变量定义与声明的区别_第1页](http://file2.renrendoc.com/fileroot_temp3/2021-11/19/c54660ee-b10a-4d07-89bb-9ac987524643/c54660ee-b10a-4d07-89bb-9ac9875246431.gif)
![变量定义与声明的区别_第2页](http://file2.renrendoc.com/fileroot_temp3/2021-11/19/c54660ee-b10a-4d07-89bb-9ac987524643/c54660ee-b10a-4d07-89bb-9ac9875246432.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、变量定义与声明的区别 变量定义与声明的区分 关于定义与声明 *begin* 变量定义与声明的区分 变量的声明有两种状况: (1) 一种是需要建立存储空间的(定义、声明)。例如:int a在声明的时候就已经建立了存储空间。 (2) 另一种是不需要建立存储空间的(声明)。例如:extern int a其中变量a是在别的文件中定义的。 前者是定义性声明(defining declaration)或者称为定义(definition),而后者是引用性声明(referncing declaration)。从广义的角度来讲声明中包含着定义,但是并非全部的声明都是定义,例如:int a它既是声明,同时又是定义
2、。然而对于extern a来讲它只是声明不是定义。一般的状况下我们经常这样叙述,把建立空间的声明称之为定义,而把不需要建立存储空间称之为声明。很明显我们在这里指的声明是范围比较窄的,也就是说非定义性质的声明。 例如:在主函数中 int main() extern int a; /这是个声明而不是定义,声明a是一个已经定义了的外部变量 /留意:声明外部变量时可以把变量类型去掉如:extern a; dosth(); /执行函数 int a; /是定义,定义了a为整型的外部变量(全局变量) 外部变量(全局变量)的定义与外部变量的声明是不相同的,外部变量的定义只能有一次,它的位置是在全部函数之外,而
3、同一个文件中的外部变量声明可以是多次的,它可以在函数之内(哪个函数要用就在那个函数中声明)也可以在函数之外(在外部变量的定义点之前)。系统会依据外部变量的定义(而不是依据外部变量的声明)安排存储空间的。对于外部变量来讲,初始化只能是在定义中进行,而不是在声明中。所谓的声明,其作用,是声明该变量是一个已在后面定义过的外部变量,仅仅是在为了提前引用该变量而作的声明而已。extern只作声明,不作定义。 用static来声明一个变量的作用有二: (1) 对于局部变量用static声明,则是为该变量安排的空间在整个程序的执行期内都始终存在 (2) 外部变量用static来声明,则该变量的作用只限于本文
4、件模块 *the end* *begin* 变量定义与声明的区分 declarations and definitions as we'll see in section 2.9 (p. 67), c+ programs typically are composed of many files. in order for multiple files to access the same variable, c+ distinguishes between declarations and definitions. 就像我们在2.9 (p. 67)节看到的一样,典型的c+程序通常会由好
5、多文件组成。为了使不同的文件都可以访问同一个变量,c+会区分变量的声明 (declarations)和定义(definitions)。 a definition of a variable allocates storage for the variable and may also specify an initial value for the variable. there must be one and only one definition of a variable in a program. 变量的定义(definitions)会为这个变量安排存储空间,并且可能会为其指定一个初始
6、化的值。在程序里,一个变量必需有一个,也只能有一处定义(definitions)。 a declaration makes known the type and name of the variable to the program. a definition is also a declaration: when we define a variable, we declare its name and type. we can declare a name without defining it by using the extern keyword. a declaration that
7、 is not also a definition consists of the object's name and its type preceded by the keyword extern: 变量的声明(declarations)会将变量的类型和名称传达给程序。当然,定义(definitions)也是一种声明:当我们定义一个变量的时候,我们当然也声明白他的名称和类型。我们可以通过使用“extern”关键字来声明(declarations)一个变量,而不用定义(definitions)它。声明(declarations)的形式就是在对象(变量)的名字和类型前面,加上关键字“ex
8、tern”: an extern declaration is not a definition and does not allocate storage. in effect, it claims that a definition of the variable exists elsewhere in the program. a variable can be declared multiple times in a program, but it must be defined only once. 变量定义与声明的区分 带“extern”关键字的语句属于声明(declaration
9、s),不是定义(definitions),他不会给变量安排内存。实际上,它是说明程序中的某处包含这个变量的定义。一个变量可以在程序中被声明(declarations)多次,但是只能被定义(definitions)一次。 a declaration may have an initializer only if it is also a definition because only a definition allocates storage. the initializer must have storage to initialize. if an initializer is prese
10、nt, the declaration is treated as a definition even if the declaration is labeled extern: 声明(declarations)时你可以给变量初始化。但是一旦你这样做,那么这句话也就变成了定义(definitions),由于只有在定义(definitions)的时候才会为变量安排内存。初始化的时候必定要为初始值安排存储空间。假如你在声明(declarations)的时候同时初始化了变量,即便“extern”关键字存在,这个语句也会认为是定义(definitions)。 despite the use of ex
11、tern, this statement defines pi. storage is allocated and initialized. an extern declaration may include an initializer only if it appears outside a function. 不管有没有“extern”关键字存在,这条语句的作用也是定义(definitions)“pi”。变量已经被安排了内存,并且给予了初始值。声明(declarations)只有在一种状况下可以被初始化,那就是当他被放置在函数外部的时候。 because an extern that i
12、s initialized is treated as a definition, any subseqent definition of that variable is an error: 由于包含初始化的声明(declarations)语句会被认为是定义(definitions),所以如下的用法会被认为是错误的: similarly, a subsequent extern declaration that has an initializer is also an error: 变量定义与声明的区分 同样的,定义(definitions)后再使用同样的声明(declarations)也
13、是错误的: the distinction between a declaration and a definition may seem pedantic but in fact is quite important. 声明(declarations)和定义(definitions)之间的区分看似有些卖弄学问的嫌疑,但是其实是特别重要的。 note in c+ a variable must be defined exactly once and must be defined or declared before it is used. 笔记 在c+里,变量必需被定义一次,最多一次,至少一
14、次,而且必需在使用前 定义(definitions)或者声明(declarations)。 any variable that is used in more than one file requires declarations that are separate from the variable's definition. in such cases, one file will contain the definition for the variable. other files that use that same variable will contain declara
15、tions for but not a definition of that same variable. 任何一个在多个文件中都要用到的变量都需要在没有其定义(definitions)的文件里对其进行声明(declarations)。在这样的状况下,一般一个文件会包含该变量的定义(definitions)。其他用到该变量的文件就会包含该变量的声明(declarations),而不是定义(definitions)。 *the end* *begin* declaration reforms programs to know the called function,whereas definit
16、ion let the compiler allocate a certain entity for the function.a function could be declared many times while definition is unique.a definition must contain a function body:/./ *the end* *begin* 小谈声明(declaration)和定义(definition) -written by kernelxu(kernelxu) 变量定义与声明的区分 -大部分内容来自博客小鱼研发手记的文章变量的定义和声明的差别
17、 (/leven/archive/2021/06/16/44793.aspx) 今日读到林锐的书高质量c/c编程指南,对其中的声明和定义内容颇有微辞。 声明(declaration)用于说明每个标识符的含义,而并不必为每个标识符预存储空间。预留存储空间的声明称为定义(definition)。声明的形式为:声明说明符 声明符声明符是由存储类说明符和类型说明符组成的。 1、变量的声明有两种状况: 一种是需要建立存储空间的。 例如:int a 在声明的时候就已建立了存储空间。 2、另一种是不必建立存储空间。 例如:extern int a 其中 变量a是在别的文件中定义的。 例一: declarat
18、ion. a construct which associates attributes to a variable name or function. no storage is reserved. for example: extrn int a; extrn char c; variable declaration a structure decleration could look like: definition. variable definition is a declaration with storage allocation. a construct which specifies the name,parameters and return type of a function. for example a function definition would be: 变量定义与声明的区分 前者是定义性声明(defining declaration)或称为定义(definition),而后者是引用性声明(referncing declaration) 。从广义的角度来讲
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人教版七年级数学上册:2.1《整式》听评课记录5
- 五年级上册数学听评课记录《4.5 探索活动:梯形的面积》(3)-北师大版
- 中图版地理七年级下册《第五节 黄土高原》听课评课记录5
- 青岛版八年级上册数学听评课记录《3-3分式的乘法与除法》
- 小学二年级数学口算速算试题
- 小学二年级第一学期班主任工作总结
- 五年级口算题带答案
- 浙教版数学七年级下册3.2《单项式的乘法》听评课记录
- 粤人版地理八年级下册《第一节 地理区域》单元整体听课评课记录2
- 听评课记录三年级语文
- 云南省普通初中学生成长记录模板-好ok
- SB/T 10415-2007鸡粉调味料
- JB/T 20036-2016提取浓缩罐
- 考古绘图基础
- GB/T 3452.4-2020液压气动用O形橡胶密封圈第4部分:抗挤压环(挡环)
- GB/T 32574-2016抽水蓄能电站检修导则
- 《社会主义市场经济理论(第三版)》第十三章社会主义市场经济标准论
- 变更索赔案例分析
- 2022年4月自学考试06093《人力资源开发与管理》历年真题及答案
- 《花婆婆》儿童绘本故事
- DB44∕T 2149-2018 森林资源规划设计调查技术规程
评论
0/150
提交评论