注意c的长度一定要能够容纳要转换的数字的长度。
比如下面的例子,c的长度至少为8(“1234567”+“\0”)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char a[20]="number:";
char c[10];
itoa(1234567,c,10);
strcat(a,c);
printf("%s\n",a);
}
用sprintf更简单,而且不会因为C99问题而无法使用
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char a[8]="0123";
const char* b = "4567";
int c = 89;
char d[50]="会从第一个字符开始覆盖";
sprintf(d,"a是:%s,b是:%s,c是:%d",a,b,c);
printf("%s\n",d);
}
输出
a是:0123,b是:4567,c是:89