三、储 存 类 别

来源:互联网 发布:dbc传奇数据库 编辑:程序博客网 时间:2024/04/28 18:11

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