簡體   English   中英

static function 中的 static 的聲明和定義中是否需要 static 關鍵字?

[英]Is the static keyword needed in both the declaration and the definition of a static function in C?

In C, inside a implementation file, when forward declaring a static function in that same file, is the static keyword needed in both the declaration (prototype) and the definition of the function?

如果您在原型中包含static (前向聲明),那么您可以在實際定義中省略該關鍵字(然后隱含)。 但是,如果您在原型中沒有static其包含在定義中,那么您處於非標准 C 的領域。

例如,以下代碼是非標准的:

#include <stdio.h>

void foo(void); // This declares a non-static function.

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

static void foo(void)
{
    printf("Foo!\n");
}

clang-cl 編譯器對此發出警告:

警告:將非靜態“foo”重新聲明為 static 是 Microsoft 擴展 [-Wmicrosoft-redeclare-static]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM