C語言中的靜態變數



預設情況下,C 變數 被歸類為自動儲存型別。當您希望在對不同函式的呼叫之間保留某個值時,靜態變數很有用。靜態變數也用於儲存應在多個函式之間共享的資料。

靜態變數

靜態變數屬於靜態儲存類別,它們只初始化一次,並保留值直到程式結束,static關鍵字用於宣告靜態變數。

靜態變數的特性

以下是 C 程式語言中靜態變數的一些特性:

  • 編譯器在計算機的主記憶體中為靜態變數分配空間。
  • 與 auto 不同,靜態變數初始化為零而不是垃圾值。
  • 如果靜態變數在函式內部宣告,則在每次函式呼叫時都不會重新初始化。
  • 靜態變數具有區域性作用域。

宣告靜態變數

要在 C 語言中宣告靜態變數,請使用static關鍵字並分配初始值。以下是宣告靜態變數的語法:

static datatype var = value;

這裡,

  • 資料型別表示變數的型別,如 int、char、float 等。
  • 變數是使用者給定的變數名。
  • 是賦予變數的任何值。預設為零。

C語言中靜態變數的示例

示例:使用靜態變數

以下是如何在 C 語言中使用靜態變數的示例:

#include <stdio.h>

int main(){

   auto int a = -28;
   static int b = 8;

   printf("The value of auto variable: %d\n", a);
   printf("The value of static variable b: %d\n",b);

   if(a != 0)
      printf("The sum of static variable and auto variable: %d\n",(b+a));
   
   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

The value of auto variable: -28
The value of static variable b: 8
The sum of static variable and auto variable: -20

示例:建立不使用靜態變數的計數器函式

在此示例中,x預設情況下是自動變數,並且在每次呼叫 counter() 函式時都初始化為 0。在每次後續呼叫中,它都會被重新初始化。

#include <stdio.h>

int counter();

int main(){
   counter();
   counter();
   counter();
   return 0;
}

int counter(){
   int x;
   printf("Value of x as it enters the function: %d\n", x);
   x++;
   printf("Incremented value of x: %d\n", x);
}

輸出

執行程式碼並檢查其輸出:

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1

但是,當 counter() 函式中的變數x宣告為static時,在第一次呼叫 counter() 函式時將其初始化為“0”。在每次後續呼叫中,它不會被重新初始化。相反,它保留了之前的值。

示例:使用靜態變數建立計數器

將“x”的宣告更改為“static int x = 0;”並再次執行程式:

#include <stdio.h>

int counter();

int main(){
   counter();
   counter();
   counter();
   return 0;
}

int counter(){
   static int x = 0;
   printf("Value of x as it enters the function: %d\n", x);
   x++;
   printf("Incremented value of x: %d\n", x);
}

輸出

現在,執行此程式碼時,將產生以下輸出:

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 1
Incremented value of x: 2
Value of x as it enters the function: 2
Incremented value of x: 3

將靜態變數傳遞給函式

您可以將靜態變數傳遞給函式。但是,形式引數不能宣告為靜態,因為 C 使用函式引數作為函式內部的區域性自動變數。

示例

在此程式碼中,我們將靜態變數傳遞給函式。但是,其值的變化不會反映在呼叫函式中。

#include <stdio.h>

int myfunction(int x);

int main(){

   static int x = 5;
   myfunction(x);
   printf("in main - x:%d\n", x);

   return 0;
}

int myfunction(int x){
   x++;
   printf("Incremented value of x: %d\n", x);
}

輸出

執行程式碼並檢查其輸出:

Incremented value of x: 6
in main - x:5

靜態變數和全域性變數之間的相似之處

靜態變數與全域性變數有一些相似之處。如果兩者都沒有顯式初始化,則都初始化為“0”(對於數字型別)或“空指標”(對於指標)。

靜態變數的作用域僅限於其宣告的函式或塊。這與全域性變數不同,全域性變數可以在整個程式中訪問。此外,靜態變數可以匯入到另一個程式碼檔案中,就像我們使用extern關鍵字一樣。

示例

您也可以將全域性變數宣告為靜態。請檢視以下示例:

#include <stdio.h>

int myfunction();

static int x = 5;

int main(){
   myfunction(x);
   printf("Inside the main function, x: %d\n", x);
   return 0;
}

int myfunction(){
   x++;
   printf("Incremented value of x: %d\n", x);
}

輸出

執行此程式碼時,將產生以下輸出:

Incremented value of x: 6
Inside the main function, x: 6

最好使用靜態變數使其只能在檔案中訪問。另一方面,使用全域性(帶extern)變數使其可以在程式的任何地方訪問(如果在其他檔案中宣告為extern)。

廣告