C/C++語言教學-儲存類別

1.自 動 變 數 ( auto variable )
        自 動 變 數 只 在 它 所 定 義 的 區 塊 內 有 效 。 只 要 在 變 數 所 屬 的 區 塊 結 構 內 執 行 , 該 變 數 的 資 料 是 有 效 而 正 確 的 。 當 程 式 執 行 離 開 了 該 區 塊 , 所 有 於 區 塊 內 定 義 的 自 動 變 數 就 不 存 在 了 。
Example 1:

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

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


2.靜 態 變 數 ( 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 = ?????
 
3.外部變數 ( 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);
}/* ======================================== */



 

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json