INT转换为char *在标准C(不itoa) [英] convert int to char* in standard C (without itoa)
本文介绍了INT转换为char *在标准C(不itoa)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经声明和初始化两个变量,如下所示:
I have declared and initialized two variables as shown below:
int a=5;
char* str;
str = (char*)calloc(255, sizeof(char));
我要为int转换为char *标准C.我不能使用从C ++等itoa任何转换功能。
I want to convert the int to char* in standard C. I cannot use any conversion function from C++ such itoa.
我使用Ubuntu 11.10
I am using Ubuntu 11.10
推荐答案
首先, itoa
不是C ++的事情。
First of all, itoa
is not a C++ thing.
您可以简单地使用的sprintf
:
sprintf(str, "%d", a)
在实际的应用程序,你需要使用的snprintf
虽然去掉缓冲区溢出的危险:
In a real application you'll want to use snprintf
though to remove the risk of a buffer overflow:
str = malloc(16);
snprintf(str, 16, "%d", a);
和15字符的方式足以存储一个整数。
And 15 characters are way enough to store an integer.
这篇关于INT转换为char *在标准C(不itoa)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文