繁体   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