当前位置: 代码迷 >> 综合 >> 静态局部变量和后增操作符
  详细解决方案

静态局部变量和后增操作符

热度:2   发布时间:2024-01-18 16:09:22.0

 

1 /***********************************************
2*测试问题:*
3*下面的程序输出结果是什么?*
4**
5*注意:下面的程序用于测试静态变量和局部变量的区别
6*以及后增操作符的运算机制。*
7**********************************************
*/

8 #include < stdio.h >
9 /***********************************************
10*first--Demonstrationofautomatic*
11*variables.*
12**********************************************
*/

13 int first( void )
14 {
15inti=0;//Demonstrationvariable
16
17return(i++);
18}

19 /***********************************************
20*second--Demonstrationofastatic*
21*variable.*
22*********************************************
*/

23 int second( void )
24 {
25staticinti=0;//Demonstrationvariable
26
27return(i++);
28}

29
30 int main()
31 {
32intcounter;//Callcounter
33
34for(counter=0;counter<3;counter++)
35printf("First%d/n",first());
36
37for(counter=0;counter<3;counter++)
38printf("Second%d/n",second());
39
40return(o);
41}

局部变量的生命周期局限于一个区域之内,离开了它的生命周期,它就不复存在。函数first()中定义的i就是这样一个变量,所以每次程序进入first函数时,i都会被初始化为0,因而,无论调用该函数多少次,函数的输出结果都是相同的。而静态变量能够保证在每次调用时变量的值能保存下来,因而如果函数中改变了该变量的值,则如果多次调用该函数,静态变量的值会不同,所以调用second函数时,输出结果每次都不相同。second函数中的i既是静态变量,又是局部变量,为什么它还是能保存变量的值呢?这就要从函数调用说起。函数每次调用时,系统都会在栈上为局部变量分配空间,而函数调用结束后,系统为函数在栈上分配的空间也就随之释放掉,局部变量的值也就无法保存下来。而当系统遇到static时,它会为该静态变量在程序的静态存储区分配空间,因而即使函数退出,函数所使用的栈空间都被释放掉了,静态变量的值仍能保存下来。
说完了静态变量,我们再来说说后增操作符operator ++。后增操作符的运算机制(以i++为例)是:1.保存i的值;2.i加一;3.返回保存的值。我们可以看到,在对变量进行后增操作时,会引入一个临时空间存储i的值,而这通常会影响程序执行效率。所以如果不需要保存i的值而只是对i进行累加操作,我们应该使用++i,而不应该使用i++,关于这一点,可以参看《exceptional c++》item6。
分析了两个关键点后,我们可以给出程序的运行结果了:

First 0
First
0
First
0
Second
0
Second
1
Second
2

public class Test  { 
 public static void main(String[] args) {
  Test test = new Test();
  System.out.println(test.f());
 }
 
 public  int  f(){
   int i=0;
  return i++;
      } 
}

结果为:0

public class Test  { 
 public static void main(String[] args) {
  Test test = new Test();
  System.out.println(test.f());
 }
 
 public  int  f(){
   int i=0;
  return ++i;
      } 
}

结果为:1

public class Test  { 
 public static void main(String[] args) {
  Test test = new Test();
  System.out.println(test.f());
 }
 
 public  int  f(){
   int i=0;
   ++i;
  return i;
      } 
}

结果为:1

public class Test  { 
 public static void main(String[] args) {
  Test test = new Test();
  System.out.println(test.f());
 }
 
 public  int  f(){
   int i=0; 
   i++;
  return i;
      } 
}

结果为:1