gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:38:17 32 4
gpt4 key购买 nike

也许不是一个真正重要的问题,但只是从 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);
}

我一直收到错误消息

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

最佳答案

改用 sprintf,itoa 不是 C 标准的一部分,它可能不包含在您的 stdlib 实现中。

sprintf(word,"%d",i);

更新:

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

关于c - itoa 将整数转换为字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5575057/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com