字符串转浮点型值
atof() 将字符串转换为双精度浮点型值
atoi() 将字符串转换为整型值
atol() 将字符串转换为长整型值
strtod() 将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字
strtol() 将字符串转换为长整值,并报告不能被转换的所有剩余数字
strtoul() 将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字
浮点型值转换为字符串:
函数名 作 用
ecvt() 将双精度浮点型值转换为字符串,转换结果中不包含十进制小数点
fcvt() 以指定位数为转换精度,余同ecvt()
gcvt() 将双精度浮点型值转换为字符串,转换结果中包含十进制小数点
itoa() (表示 integer to alphanumeric)是把整型数转换成字符串的一个函数。char* itoa(int value,char*string,int radix);//value: 要转换的整数,string: 转换后的字符串,radix: 转换进制数,如2,8,10,16 进制等。只存在于windows中
注意:linux中用sprinf函数代替
int nValue = 80;
char* szBuffer = (char *)malloc(sizeof(int) + 1); //分配动态内存
memset(szBuffer, 0, sizeof(int) + 1); //内存块初始化
sprintf(szBuffer, "%d", nValue); //整数转化为字符串
free(szBuffer); //释放动态分配的内存
c语言在输出的时候可以补零
//要得到输出:004
int a = 4;
printf("%03d",a);
//也可以用 * 代替位数,在后面的参数列表中用变量控制输出位数;
int a = 4;
int n = 3;
printf("%0*d",n,a);
测试1
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int num;
char * str = "65535";
num = atoi(str);
printf("The string 'str' is %s and the number 'num' is %d. \n",str, num);
return 0;
}
输出1
The string 'str' is 65535 and the number 'num' is 65535.
测试2
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
unsigned int num=837426140;
char *str;
itoa(num,str,10);
printf("The string 'str' is %s and the number 'num' is %d. \n",str, num);
return 0;
}