軌道
/
C
C
/
課程大綱
/
靜態函式
靜態

靜態函式 在 C

{other: "%{count} 個練習"}

關於 靜態函式

函式預設具有外部連結,可以從專案中的任何其他檔案呼叫。想進一步了解連結,請參閱連結概念。static函式只能在定義它的原始檔內呼叫。

例如

// file scope, internal linkage
// can only be called from within this file
static void some_function()
{
    // code snipped
}

// file scope, external linkage by default
// can be called from any other file in the project
int main()
{
    // code snipped
}

即使函式宣告在標頭檔中,只要將它定義為static,就無法從它的原始檔外部呼叫。原始檔可以引入標頭檔、呼叫標頭檔中宣告的static函式並通過編譯,但連結器會找不到static函式的定義。在原始檔中標記為static的函式,不應該在它的標頭檔中宣告。

透過 GitHub 編輯 連結會在新視窗或分頁中開啟