C语言中的变量

来源:互联网 发布:新倩女幽魂辅助软件 编辑:程序博客网 时间:2024/04/30 00:53

本文主要用以解释以下问题:

1)automatic variable(自动变量) 和 static variable(静态变量)的区别?
2)static变量放在function内部和外部的区别?
3)definition(定义)和declaration(声明)有什么区别?

1:什么是variable(变量)

变量有以下这两个定义:

A program variable is an abstraction of a computer memory cell or collection of cells;
A variable can be characterized as sextuple of attributes: name, address, value, type, lifetime, scope

搞清这点,尤其是第二点,对后面理解变量之间的差异至关重要。

2:variable types(变量类型)

你的第一反应可能是int、char?double?不,这些都不是,这些是data types(数据类型)。C语言的变量类型主要有两种:

local variables (局部变量)
global variables (全局变量)

而extern、static又是什么呢?storage classes,可以翻译成存储类别。

3: auto(automatic)变量

auto variable,经常有人翻译成自动变量。
auto is the default variable for all local variables.
记住一句话,所有的local variables都是auto variables。

4:register变量

寄存器变量,首先还是局部变量。
Register is used to define local variables that should be stored in a register instead of RAM.
利用寄存器变量可以快速完成的变量的存取。从寄存器取数显然要比从内存中快!但是,它有两个很严重的限制:

1) maximum size equal to the register size;
2) cannot have ‘&’ operator applied to it(it has no memory location).

5:全局变量

通俗的说法是,定义在函数外部的变量为全局变量。全局变量可以为本project中其他函数所共用,它的有效范围从定义变量的位置开始到本源文件结束。建立全局变量的作用是增加了函数间数据联系的渠道。

extern关键字可以将全局变量的作用域扩展至其他源程序。在应用之前用关键字extern对该变量作‘外部变量声明’。表示该变量是一个已经定义了的全局变量。有了此声明,就可以从声明处起,合法的调用该外部变量。

与static全局变量的区别:通常static全局变量应用于程序中希望某些全局变量只限于被本文件引用,而不能被其他文件引用。即static全局变量的作用域只在本文件。

6: static变量

静态变量,对于大多数人它的迷惑性是函数外的static和函数内的static有什么区别?

定义:static is the default storage class for global variables.

即所有未加修饰的local variable都是auto的,而所有未加修饰的global variable都是static的!看下面的代码;

static int Count;  int Road;int main()  {      printf("%d\n", Road);      return 0;} 

这两个变量都是static的(一个静态全局变量,一个全局变量)!因为没有被初始化,他们存放在bss区,并且将被自动初始化为0。

但是,如果static放在函数内部会有什么区别呢?
还记得刚开始提到变量的sextuple吗?也就是name, address, value, type, lifetime, scope。
区别在于scope(作用域)。如果static放在function外面,那么就具有全局作用域。如果放在函数内部,就只有局部作用域。但不管是局部static还是全局static,他们的lifetime(生命周期)都是一样的,一直持续到整个源程序的结束。

一个可能经常会考察到的例子:

char *func(void);  main()  {     char *Text;     Text = func();  }  char *func(void)  {     /*case 1: */      char Text1[10]="martin";     /*case 2: */      static char Text2[10]="martin";     return(Text1);  }  

试想,如果执行case 1的情况会return什么?不确定!想想变量的lifetime。在case 1中 char *Text是自动(局部)变量,return过后随即就被清除,所以返回什么不确定!但是case 2中,static char *Text2的lifetime是整个程序的执行过程,当然会正常返回!这一点经常被考察,其实就是考你生命周期和存储位置的相关知识。

7:extern变量

在解释extern之前,先得把一个变量或函数的definition和declaration了解清楚!

Definition means a variable or function is defined in reality and actual memory is allocated.
Declaration means just giving a reference of a variable or function.

声明和定义都规定了变量的类型,名字,区别主要在于定义是分配了空间,声明只是对编译器说:这个变量或函数在其它地方已经定义过,并且将在连接时提供给你。

Extern is used to give a reference of a global variable that is visible to ALL program files.

注意是global,凡是带extern的都不能被初始化(When you use ‘extern’ the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.)若初始化,将变成定义了,可能与你原本的意图(引用但不改变其值相违背)。因为在别处已经定义好了,extern只是把它引用过来罢了!

本文主要参考自:http://blog.csdn.net/blitzskies/article/details/42499901