怎样把数字转换为字符串 与 atoi 相反 ?? 有 itoa 函数吗 ??
itoa 在 windows 下有,非标准函数库。。
其实我们可以用 sprintf 实现
sprintf (string, "%d", number);
也可以用 sprintf把long型或者浮点型转换为字符串型 %ld %f
所以说 sprintf 是 atoi 的反函数
- #include <stdio.h>
-
#include <stdlib.h> // EXIT_SUCCESS
-
-
#define SIZE 1024
-
-
int main(int argc, char *argv[])
-
{
-
char str[SIZE];
-
int number;
-
-
printf("input the number\n");
-
scanf("%d", &number);
-
-
#if 1
-
if(sprintf(str,"%d",number) == -1)
-
{
-
perror("failed sprintf\n");
-
exit(EXIT_FAILURE);
-
-
}
-
#endif
-
printf("after sprintf str is %s\n\n",str);
-
-
int year;
-
int month;
-
int day;
-
-
printf("input the year:\n");
-
scanf("%d", &year);
-
-
printf("input the month:\n");
-
scanf("%d", &month);
-
-
printf("input the day:\n");
-
scanf("%d", &day);
-
-
sprintf(str,"year:%d--month:%d--day:%d", year, month, day);
-
printf("after sprintf str= %s\n", str);
-
-
exit(EXIT_SUCCESS);
-
}
- ywx@yuweixian:~/yu/c/test$ ./test
-
input the number
-
234
-
after sprintf str is 234
-
-
input the year:
-
2011
-
input the month:
-
6
-
input the day:
-
5
-
after sprintf str= year:2011--month:6--day:5
阅读(2012) | 评论(0) | 转发(0) |