definition vs declaration of variables
来源:互联网 发布:php前端和后端的区别 编辑:程序博客网 时间:2024/05/24 00:27
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.
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 is not also a definition consists of the object's name and its type preceded by the keyword extern:
extern int i; // declares but does not define i int i; // declares and defines i
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.
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 present, the declaration is treated as a definition even if the declaration is labeled extern:
extern double pi = 3.1416; // definition
Despite the use of extern, this statement defines pi. Storage is allocated and initialized. An extern declaration may include an initializer only if it appears outside a function.
Because an extern that is initialized is treated as a definition, any subseqent definition of that variable is an error:
extern double pi = 3.1416; // definition double pi; // error: redefinition of pi
Similarly, a subsequent extern declaration that has an initializer is also an error:
extern double pi = 3.1416; // definition extern double pi; // ok: declaration not definition extern double pi = 3.1416; // error: redefinition of pi
The distinction between a declaration and a definition may seem pedantic but in fact is quite important.
- definition vs declaration of variables
- 关于variables declaration 和definition 的一篇文章
- The issue of template definition and declaration separation
- function declaration and definition
- declaration and definition ?
- 声明和定义 Declaration & Definition
- C++: 为什么我们一般都要将函数的声明和定义分开?(separate the declaration of function from its definition)
- Diffrent ways to travers buildetails of a build definition with vs 2005 and vs 2008
- Definition(定义) and Declaration(声明)
- K&R C Bible Declaration & definition
- 声明(declaration),定义(definition),以及初始化(initialization)
- C++中definition与declaration的区别
- declaration(声明)与definition(定义)
- Properties vs instance variables
- Summary of Variables
- Specifying Attributes of Variables
- the variables of python
- Specifying Attributes of Variables
- 设计模式c#语言描述——装饰(Decorator)模式
- Matrix Template Library Help
- 关于ORM实体映射的感想 (Object /Relational Mapper) GOOD!
- initialize the variables!!!
- ASP.NET中TreeView控件使用小结
- definition vs declaration of variables
- [转载].Net网络资源
- 微软的O/R映射技术--Visual Studio "Whidbey"的对象空间初识
- nhibernate源码分析之一: 对象映射
- 在ASP.NET中实现多文件上传
- .Grove—— .Net下的ORM框架
- -在javascript中获取机器名和window登录名
- TCP-IP学习笔记_卷1_第12章_多播和广播
- JAVA相关基础知识---2