注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Leo

笑:胸怀,傲:实力,才能笑傲江湖。

 
 
 

日志

 
 

静态变量的实例用法  

2011-03-07 20:31:41|  分类: 大学留笔 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1、      

                           #include<stdio.h>
                                 static int x=5;
                                 void f1(),f2();
                                 main()
                                {
                                        auto int x=3;
                                        printf("1.x=%d\n",x);
                                        f1();
                                        f2();
                                        printf("4.x=%d\n",x);
                                 }
                                void f1()
                                {
                                        x+=1;
                                        printf("2.x=%d\n",x);
                                 }
                                 void f2()
                                 {
                                         int x=1;
                                         printf("3.x=%d\n",x);
                                  }

x是静态变量,等价于全局变量,另外main()和f2()定义了局部变量,结果:

1.x=3

2.x=6

3.x=1

4.x=3

 

 

2、

                                                                             #include<stdio.h>
                                                                             void func();
                                                                             int a=1,j=2;
                                                                             main()
                                                                            {
                                                                                   int i=2;
                                                                                   func();
                                                                                   printf("a=%d,i=%d,j=%d\n",a,i,j);
                                                                             }
                                                                            void func()
                                                                             {
                                                                                    int n,i=5;
                                                                                    printf("a=%d,i=%d\n",a,--i);
                                                                                    i++;
                                                                                    a++;
                                                                                    if(a<4)
                                                                                    {
                                                                                         func();
                                                                                     }
                                                                                     a--;
                                                                                     j+=3;
                                                                                     printf("a=%d,j=%d\n",a,j);
                                                                               }

运行结果:

a=1,i=4

a=2,i=4

a=3,i=4

a=3,j=5

a=2,j=8

a=1,j=11

a=1,i=2,j=11

  评论这张
 
阅读(279)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017