<分区>
以下代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
long long int a;
scanf("%lld", &a);
printf("%lf", sqrt(a));
return 0;
}
给出输出:
source_file.c: In function ‘main’:
source_file.c:9:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a);
^
/tmp/ccWNm2Vs.o: In function `main':
source.c:(.text.startup+0x65): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
但是,如果我执行 long long int a = 25;
并删除 scanf
语句,或者只是执行 sqrt(25)
,它们都有效(正确给出输出 5.000000
)。
我检查了this question , 但它适用于 C++ 并使用函数重载,而 afaict C 没有函数重载(这就是为什么我们有 sqrtf
、sqrt
、sqrtl
如果我没记错的话)。此外,无论我采用 long long int
还是 double
类型的 a
,上述代码都会失败。所以,这些问题可能不相关。
另外,关于其他 linked question ,对于不断定义的值,我没有发生错误,而恰好是链接问题的情况。
那是什么原因呢?为什么常量值适用于 sqrt
,而可变用户输入值却不行?