C标准(C99+)是否要求实现允许将函数声明放在块中,以便将其范围限制在块and where is this covered in the standard内?假设该函数具有外部链接,并在链接时包含的单独源文件中定义.
我注意到,在编译以下程序时,GCC会生成一个错误:
int main(void)
{
void myFunc(void);
myFunc();
return 0;
}
void test2(void)
{
myFunc();
}
错误(这是预期的):
..\main.c: In function 'test2':
..\main.c:12:3: warning: implicit declaration of function 'myFunc' [-Wimplicit-function-declaration]
12 | myFunc();
| ^~~~~~
..\main.c:3:8: note: previous declaration of 'myFunc' was here
3 | void myFunc(void);
| ^~~~~~
..\main.c:12:3: error: incompatible implicit declaration of function 'myFunc'
12 | myFunc();
| ^~~~~~
..\main.c:3:8: note: previous implicit declaration of 'myFunc' was here
3 | void myFunc(void);
| ^~~~~~
这是意料之中的,因为myFunc()
是在main()
的范围内宣布的.
如果从test2()
中删除了对myFunc()
的调用,并且在链接时包含的另一个源文件中定义了test2()
,则程序编译并链接时不会出现错误或警告.
基于这个结果,这个问题的答案是肯定的,但我想知道这个行为是否在规范中明确定义,是否可以被认为是可移植的.