在 C 语言中,在实现文件中,当在同一个文件中前向声明静态函数时,声明(原型(prototype))和函数定义中都需要 static 关键字吗?
最佳答案
如果您包括 static
在原型(prototype)(前向声明)中,您可以在实际定义中省略该关键字(然后隐含)。但是,如果您没有 static
在原型(prototype)中但确实将其包含在定义中,那么您就处于非标准 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 - C 中静态函数的声明和定义都需要 static 关键字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67574432/