C語言中的static關鍵字



什麼是C語言中的static關鍵字?

C語言中的**static關鍵字**是儲存類別說明符之一,其含義取決於其使用環境。

“**static**”關鍵字用於宣告靜態變數以及定義靜態函式。當宣告為“static”時,變量表示靜態儲存類別。

**靜態函式**僅在其定義所在的程式檔案(副檔名為“.c”)中可用。不能使用“extern”關鍵字將靜態函式匯入到另一個檔案中。

static關鍵字的用途

以下是static關鍵字的不同用途:

  • **靜態區域性變數**: 當用static關鍵字宣告區域性變數時,其生命週期將持續到程式結束,並且它在函式呼叫之間保留值。
  • **靜態全域性變數**: 當用static關鍵字宣告全域性變數時,它只能在同一個檔案中訪問。當您想將全域性變數作為其宣告檔案私有的全域性變數時,這很有用。
  • **靜態函式**: 當函式被宣告為靜態函式時,其作用域將限制在其宣告所在的 檔案中。您不能在其他檔案中訪問該函式。

靜態變數(使用關鍵字static宣告變數)

**當變數宣告為static時,它只初始化一次**。編譯器將該變數保留到程式結束。一個靜態變數也用於儲存應在多個函式之間共享的資料。

以下是關於靜態變數的一些重要注意事項:

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

static關鍵字與變數的示例

在下面的示例中,counter()函式中的變數“x”宣告為static。第一次呼叫counter()函式時,它被初始化為“0”。在每次後續呼叫中,它不會重新初始化;而是保留之前的值。

#include <stdio.h>

int counter();

int main() {

   counter();
   counter();
   counter();
   return 0;
}

int counter() {
   static 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: 1
Incremented value of x: 2
Value of x as it enters the function: 2
Incremented value of x: 3

靜態變數類似於全域性變數,因為它們都初始化為0(對於數值型別)或空指標(對於指標),除非顯式賦值。但是,靜態變數的作用域僅限於其宣告的函式或塊。

靜態函式(使用關鍵字static宣告函式)

預設情況下,每個函式都被編譯器視為全域性函式。它們可以在程式的任何地方訪問。

在定義中使用關鍵字“static”作為字首時,我們將得到一個靜態函式,其作用域僅限於其目標檔案(儲存為“.c”副檔名的程式的編譯版本)。這意味著靜態函式僅在其目標檔案中可見。

靜態函式可以透過在函式名前放置關鍵字“static”來宣告。

static關鍵字與函式的示例(在多個檔案中)

使用CodeBlocks IDE開啟一個控制檯應用程式。新增兩個檔案“file1.c”和“main.c”。這些檔案的內容如下:

“**file1.c**”的內容:

static void staticFunc(void) {
   printf("Inside the static function staticFunc() ");
}

“**main.c**”的內容:

#include <stdio.h>

#include <stdlib.h> int main() { staticFunc(); return 0; }

現在,如果構建上述控制檯應用程式專案,我們將得到一個錯誤,即“undefined reference to staticFunc()”。發生這種情況是因為函式staticFunc()是靜態函式,它只在其目標檔案中可見。

static關鍵字與函式的示例(在同一個檔案中)

下面的程式演示了靜態函式在C程式中的工作方式:

#include <stdio.h>

static void staticFunc(void){
   printf("Inside the static function staticFunc() ");
}

int main(){

   staticFunc();
   return 0;
}

輸出

上述程式的輸出如下:

Inside the static function staticFunc()

在上述程式中,函式staticFunc()是一個靜態函式,它列印“Inside the static function staticFunc()”。main()函式呼叫staticFunc()。此程式正常工作,因為靜態函式僅從其自身的目標檔案呼叫。

static關鍵字與多個函式的示例

您可以在同一個目標檔案中包含多個靜態函式,如下例所示:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// define the static function
static int square( int num){
   return num * num;
}

static void voidfn(){
   printf ("From inside the static function.\n");
}

static int intfn(int num2){
   return sqrt(num2);
}

int main(){

   int n1, val;
   n1 = 16;
   val = square(n1);	// Call voidfn static function

   printf("The square of the %d : %d\n", n1, val);
   voidfn();		// Call intfn static function
   val = intfn(n1);
   printf("The square root of the %d : %d\n", n1, val);

   return 0;
}

輸出

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

The square of the 16: 256
From inside the static function.
The square root of the 16: 4
廣告