注:
- 为了叙述的方便,以下数字均指整形,其它类型同样具有对应函数
- C语言的字符串是指字符数组,C++的字符串是指string,两者并不相同
int atoi(const char *nptr);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
scanf("%s", str);
int n;
n = atoi(str);
printf("%d\n", n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
scanf("%s", str);
int n;
sscanf(str, "%d", &n);
printf("%d", n);
return 0;
}
char *itoa (int value, char *str, int base );
int value: 被转换的整数
char *string: 转换后储存的字符数组
int base: 转换进制数,如2,8,10,16 进制等,大小在2-36之间
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
char str[10];
itoa(n, str, 10);
printf("%s\n", str);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
char str[10];
sprintf(str, "%d", n);
printf("%s", str);
return 0;
}
原文:https://www.cnblogs.com/G-H-Y/p/14742663.html