itoa将整数转换为字符串的问题

kal*_*cey 2 c string int

也许不是一个非常重要的问题,但只是从c开始.为什么这不能正确编译?

#include <stdio.h>
#include <stdlib.h>

void main()
{
int i = 15;
char word[100];

itoa (i,word,10);

printf("After conversion, int i = %d, char word = %s", i, word);
}
Run Code Online (Sandbox Code Playgroud)

我一直收到错误消息

Undefined symbols:
"_itoa", referenced from:
_main in ccubsVbJ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

GWW*_*GWW 6

使用sprintf代替,itoa不是C标准的一部分,它可能不包含在你的stdlib实现中.

sprintf(word,"%d",i);
Run Code Online (Sandbox Code Playgroud)

更新:

如注释中所述,snprintf更安全,可避免缓冲区溢出.在这种情况下,没有必要,因为缓冲区长于可以存储在32位(或64位整数)中的最大整数.但是记住缓冲区溢出是件好事.

  • 如果可用,您应该使用**snprintf()**来避免缓冲区溢出. (2认同)