结果编译时提示信息错误,为什么?

我编写了个回文数的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int i, j;
int m, n;
int f = 1;
char c[10];
for (i=1; i<257; i++)
{
m = i * i;
itoa(m, c, 10);
n = strlen(c);
for (j=0; j<n/2; j++)
{
if (c[j] != c[n-1-j])
{
f = 0;
break;
}
}
if (f)
{
printf ("%d\n", i);
}
}
return 0;
}

结果编译时提示信息:
/tmp/cc4rfntR.o: In function `main':
jc.c:(.text+0x44): undefined reference to `itoa'
collect2: ld returned 1 exit status

请问这是为什么??请高手指教,小弟不甚感激!!!

暮色呼如
浏览 146回答 2
2回答

神不在的星期二

附加 gcc 参数 -lxxxx 方式调用提供 itoa 函数接口的库。不过你需要确定这个xxxx 应该是哪个才行。你现在提示是 undefined reference 而不是提示函数未定义。所以应该代码没问题,是编译环境的函数库调用有问题。你怎么装的开发环境?不会是装了头文件没装对应 so 吧?

慕妹3242003

#include <stdlib.h>int atoi(const char *nptr);long atol(const char *nptr);long long atoll(const char *nptr);long long atoq(const char *nptr);linux下面没对应的好像,我man 没有查到.给你直接找到一个实现,你放到自己代码里面就可以了void itoa ( unsigned long val, char *buf, unsigned radix )&nbsp;&nbsp;{&nbsp;&nbsp;char *p; /* pointer to traverse string */&nbsp;&nbsp;char *firstdig; /* pointer to first digit */&nbsp;&nbsp;char temp; /* temp char */&nbsp;&nbsp;unsigned digval; /* value of digit */&nbsp;&nbsp;p = buf;&nbsp;&nbsp;firstdig = p; /* save pointer to first digit */&nbsp;&nbsp;do {&nbsp;&nbsp;digval = (unsigned) (val % radix);&nbsp;&nbsp;val /= radix; /* get next digit */&nbsp;&nbsp;/* convert to ascii and store */&nbsp;&nbsp;if (digval > 9)&nbsp;&nbsp;*p++ = (char ) (digval - 10 + 'a '); /* a letter */&nbsp;&nbsp;else&nbsp;&nbsp;*p++ = (char ) (digval + '0 '); /* a digit */&nbsp;&nbsp;} while (val > 0);&nbsp;&nbsp;/* We now have the digit of the number in the buffer, but in reverse&nbsp;&nbsp;order. Thus we reverse them now. */&nbsp;&nbsp;*p-- = '\0 '; /* terminate string; p points to last digit */&nbsp;&nbsp;do {&nbsp;&nbsp;temp = *p;&nbsp;&nbsp;*p = *firstdig;&nbsp;&nbsp;*firstdig = temp; /* swap *p and *firstdig */&nbsp;&nbsp;--p;&nbsp;&nbsp;++firstdig; /* advance to next two digits */&nbsp;&nbsp;} while (firstdig < p); /* repeat until halfway */&nbsp;&nbsp;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP