我有以下代码:
#include <stdio.h>
#include <stdlib.h>
extern void foo(double);
int main(void) {
// printf("address is: %p\n", *foo);
if (foo)
puts("indeed");
else
puts("not");
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
indeed如果第7行被注释,它总是编译和打印,但gcc警告我:
the address of 'foo' will always evaluate as 'true' [-Waddress]
Run Code Online (Sandbox Code Playgroud)
但是,如果第7行未注释,则无法编译:
/tmp/ccWvhcze.o: In function `main':
post.c:(.text.startup+0x5): undefined reference to `foo'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
好吧,如果根据gcc 地址总是非零,为什么链接失败?当然,我会期待它,因为foo这里没有定义,但为什么gcc声称地址总是非零?是否C语言强制要求翻译单元中的所有标识符始终评估为真?