【问题标题】:Is the static keyword needed in both the declaration and the definition of a static function in C?C 中静态函数的声明和定义都需要 static 关键字吗?
【发布时间】:2021-05-17 17:28:54
【问题描述】:

在 C 语言中,在实现文件中,当在同一个文件中前向声明静态函数时,声明(原型)和函数定义中都需要 static 关键字吗?

【问题讨论】:

标签: c static


【解决方案1】:

如果您在原型中包含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”重新声明为静态是 Microsoft 扩展名 [-Wmicrosoft-redeclare-static]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2021-06-21
    • 1970-01-01
    • 2014-08-22
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多