三、储 存 类 别

l        l        ( auto variable )

l          l         

l          l             Example 1:

l          l           main( )
        {
           int x=1; 
          
inner
( );
           printf("%d/n",x);
        }

l          l                     inner( )
        {
           int x=2;
           printf("%d/n",x);
        }

 

l        l        ( static variable)

但静 的执

  

      


  {
           static int a;
           static int b=12345;
           static char c;
           static float d=13.45;
               .
               .
               .
           } 

 
Example 1:
       
       
 
        
        
main()
        
        
    {
        
        
        increment();
        
        
        increment();
        
        
        increment();
        
        
    }          
        
        
    
        
        
      increment()
        
        
     {
        
        
        int x=0;
        
        
        x=x+1;
        
        
     printf("%d/n",x);
        
        
        }
        
        
 
       
       
Result = ?????
         
         
Example 2:
        
        
 
        
        
main()
        
        
   {
        
        
        increment();
        
        
        increment();
        
        
        increment();
        
        
   }
        
        
 
        
        
       increment()
        
        
   {
        
        
     static int x=0;
        
        
     x=x+1;
        
        
     printf("%d/n",x);
        
        
        }
        
        
 
        
        
Result = ?????
         
         
 
    
    

l        l        ( extern variable)

外部 ( global )
 

Example 1:

     int x=123;

         main()
        {
           printf("%d/n",x);


        }  

Result =  ?????

Example 2:

    int x=123;

   main()
        {
           int x=321;
           printf("%d/n",x);

        } 

Result =  ?????

 

Example 3:

#include < stdio.h >
#include "extern.c"

         int x=123; 
         main()
        {
           printf("%d/n",x);
           run1();
           run2(); 
         }
         run1()
        {
           printf("%d/n",x);
         }

 

extern.c :

         #include < stdio.h >
         run2()
        {
           extern int x;
           printf("%d/n",x);
         }

 

Result =  ?????

 

/* ======================================== */

/*    程序实例:                                 */

/*    局部(local)和整体(Global) 变量              */

/* ======================================== */

#include <stdio.h>

 

int step = 10;                    /* 整体变量宣告 */

int count = 5;                    /* 整体变量宣告 */

 

/* ---------------------------------------- */

/*  将变量值加一              */

/* ---------------------------------------- */

void increment()

{

   int step = 0;                    /* 局部变量 step 宣告 */

 

   step++;                        /* 局部变量加一 */

   count++;                       /* 整体变量加一 */

   printf(" 副程序    %2d      %2d    /n",step, count);

}

 

/* ---------------------------------------- */

/*  主程序                                  */

/* ---------------------------------------- */

void main()

{

   int count = 0;                   /* 局部变量宣告 */

 

   count++;                       /* 局部变量加一 */

   step++;                        /* 整体变量加一 */

   printf(" 程序名    STEP    COUNT   /n");

   increment();                   /* 副过程调用 */

   printf(" 主程序    %2d      %2d    /n",step, count);

}/* ======================================== */

 

 

执行结果

 

程序名

STEP

COUNT

副程序

1

6

主程序

11

1


 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值