Tanky WooRSS

一个误导了自己很久的问题--定义与声明

07 Mar 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

一直把

int a;

当做一个声明。

最近再给论坛出题目时,才发现自己一直都是错的。

1.这是在《C Primer Plus》上找的原版资料。

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

2.这是在网上找的一个比较统一的说法:

变量的声明有两种情况: (1) 一种是需要建立存储空间的(定义、声明)。 例如:int a在声明的时候就已经建立了存储空间。 (2) 另一种是不需要建立存储空间的(声明)。 例如:extern int a其中变量a是在别的文件中定义的。 前者是"定义性声明(defining declaration)"或者称为"定义(definition)",而后者是"引用性声明(referncing declaration)"。从广义的角度来讲声明中包含着定义,但是并非所有的声明都是定义,例如:int a它既是声明,同时又是定义。然而对于extern a来讲它只是声明不是定义。一般的情况下我们常常这样叙述,把建立空间的声明称之为"定义",而把不需要建立存储空间称之为"声明"。很明显我们在这里指的声明是范围比较窄的,也就是说非定义性质的声明。

例如:在主函数中

int main()
{
extern int A; //这是个声明而不是定义,声明A是一个已经定义了的外部变量
//注意:声明外部变量时可以把变量类型去掉如:extern A;
dosth();      //执行函数
}
int A;            //是定义,定义了A为整型的外部变量(全局变量)

外部变量(全局变量)的"定义"与外部变量的"声明"是不相同的,外部变量的定义只能有一次,它的位置是在所有函数之外,而同一个文件中的外部变量声明可以是多次的,它可以在函数之内(哪个函数要用就在那个函数中声明)也可以在函数之外(在外部变量的定义点之前)。系统会根据外部变量的定义(而不是根据外部变量的声明)分配存储空间的。对于外部变量来讲,初始化只能是在"定义"中进行,而不是在"声明"中。所谓的"声明",其作用,是声明该变量是一个已在后面定义过的外部变量,仅仅是在为了"提前"引用该变量而作的"声明"而已。extern只作声明,不作定义。

用static来声明一个变量的作用有二: (1) 对于局部变量用static声明,则是为该变量分配的空间在整个程序的执行期内都始终存在 (2) 外部变量用static来声明,则该变量的作用只限于本文件模块

3.另外,《C陷阱与缺陷》上也与比较完整的讲解。