【发布时间】:2021-07-19 19:36:59
【问题描述】:
我正在编写一个程序 C 程序来确定一个数字是否是 Armstrong 数字。 代码如下:
#include <stdio.h>
#include <math.h>
int main(){
int inp,count,d1,d2,rem;
float arm;
printf("the number: ");
scanf("%d",&inp);
d1 = inp;
d2 = inp;
// for counting the number of digits
while(d1 != 0){
d1 /= 10;
++count;
}
//for checking whether the number is anarmstrong number or not
while(d2 != 0){
rem = (d2 % 10);
arm += pow(rem,count);
d2 /= 10;
}
printf("%f",arm);
return 0;
}
(文件名:sample.c)
我希望程序将输出显示为:
the number:
但它显示以下错误:
usr/bin/ld: /tmp/ccleyeW0.o: in function `main':
sample.c:(.text+0xb4): undefined reference to `pow'
collect2: error: ld returned 1 exit status
我什至使用了命令 GCC -o sample sample.c -lm,但仍然出现错误。
所以我想知道是什么导致了这个错误,我该如何解决这个问题。
【问题讨论】:
-
-lm应该已经解决了。请删除所有.o文件,然后重试。 -
仔细检查您使用的是
-lm。缺少-lm几乎总是导致此问题的原因。 -
float arm;->float arm = 0; -
你还应该将
count初始化为0。
标签: c math flow-control