C中静态函数的声明和定义都需要static关键字吗?
Is the static keyword needed in both the declaration and the definition of a static function in C?
在 C 中,在一个实现文件中,当在同一个文件中前向声明一个静态函数时,在函数的声明(原型)和定义中是否都需要 static 关键字?
如果您在原型(前向声明)中包含 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 编译器对此发出警告:
warning : redeclaring non-static 'foo' as static is a Microsoft
extension [-Wmicrosoft-redeclare-static]
在 C 中,在一个实现文件中,当在同一个文件中前向声明一个静态函数时,在函数的声明(原型)和定义中是否都需要 static 关键字?
如果您在原型(前向声明)中包含 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 编译器对此发出警告:
warning : redeclaring non-static 'foo' as static is a Microsoft extension [-Wmicrosoft-redeclare-static]