在C INT转换为字符串 [英] Converting int to string in c

查看:128
本文介绍了在C INT转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是 itoa()功能转换的 INT 字符串但它给了一个错误

I am using the itoa() function to convert an int into string but it is giving an error

undefined reference to `itoa'
collect2: ld returned 1 exit status

有人能告诉我原因吗?
有没有执行此转换一些其他的方式?

Can someone tell me the reason? Is there some other way to perform this conversion?

推荐答案

使用的snprintf ,它更便于携带超过 itoa

Use snprintf, it is more portable than itoa.

itoa不是标准C的一部分,也不是标准的C ++ 的一部分;但是,大量的编译器和相关的库的支持。

itoa is not part of standard C, nor is it part of standard C++; but, a lot of compilers and associated libraries support it.

的sprintf

char* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);

的snprintf

char buffer[10];
int value = 234452;
snprintf(buffer, 10, "%d", value);

这两个函数类似于 fprintf中,但输出将被写入到一个数组,而不是流。的sprintf 和的snprintf 的snprintf 保障之间的的差异不缓冲器写入最多可以存储在缓冲区中的字符的最大数溢出

Both functions are similar to fprintf, but output is written into an array rather than to a stream. The difference between sprintf and snprintf is that snprintf guarantees no buffer overrun by writing up to a maximum number of characters that can be stored in the buffer.

这篇关于在C INT转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆