static的使用

 1 #include <stdio.h>
 2 
 3 void test()
 4 {
 5     static int i = 5;
 6     printf("%d\n",i);
 7 }
 8 int caculate(int a)
 9 {
10     int result = 0;
11 
12     if (a>0)
13     {
14 
15         for (int j = a; j>0; j--)
16         {
17             result = j;
18             result = j*result;
19             printf("j = %d,result=%d\n",j,result);
20         }
21     }
22         return  result;
23 
24 }
25 int staticPlus()
26 {
27 
28     static int i = 0;
29     int result = 0;
30     result = i++;
31     printf("有 static 的结果为%d\n",result);
32     return 0;
33 }
34 int plus()
35 {
36 
37     int i = 0;
38     int result = 0;
39     result = i++;
40     printf("无 static 的结果为%d\n",result);
41 //    printf("%d",j);
42     return 0;
43 }
44 static int p = 5;
45 int q = 6;
46 int main(int argc, const char * argv[]) {
47 
48 //    test();
49 //    caculate(5);
50 //    static int j = 5;
51     int x = 7;
52     printf("%d,内存地址为:%p\n",p,&p);
53     printf("%d,内存地址为:%p\n",q,&q);
54     printf("%d,内存地址为:%p\n",x,&x);
55     /**
56      *  
57      5,内存地址为:0x100001020      (此地址为栈区)
58      6,内存地址为:0x10000101c      (此地址为栈区)
59      7,内存地址为:0x7fff5fbff7ec   (此地址为堆区)
60      */
61 
62     staticPlus();
63     staticPlus();
64     plus();
65     plus();
66     /**
67      * 
68      有 static 的结果为0
69      有 static 的结果为1
70      无 static 的结果为0
71      无 static 的结果为0
72      */
73     return 0;
74 }

 

posted @ 2016-04-13 14:47  walker彬  阅读(153)  评论(0编辑  收藏  举报