![](/img/trans.png)
[英]In C How can I convert an int to a char[] without using snprintf or itoa?
[英]convert int to char* in standard C (without itoa)
我已經聲明並初始化了兩個變量,如下所示:
int a=5;
char* str;
str = (char*)calloc(255, sizeof(char));
我想在標准C中將int轉換為char *。我不能使用C ++中的任何轉換函數,例如itoa。
我正在使用Ubuntu 11.10
首先, itoa
不是C ++的東西。
你可以簡單地使用sprintf
:
sprintf(str, "%d", a)
在實際應用程序中,您將希望使用snprintf
來消除緩沖區溢出的風險:
str = malloc(16);
snprintf(str, 16, "%d", a);
15個字符足以存儲整數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.