C/C++数字与字符串之间的转换
sprintf函数实现数字与字符串的转换
sprintf()函数定义
sprintf(buffer, "a = %d", a);
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
using namespace std;
int main()
{
int a, b, c;
scanf("%d%d%d",&a, &b, &c);
printf("a = %d, b = %d, c = %d", a, b, c);
char buffer[256];
memset(buffer, '\0', 256);
sprintf(buffer, "%d", a);
cout << buffer;
system("pause");
return 0;
}
char str[10];
double a=123.321;
sprintf(str,"%.3lf",a);
==itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。==