sdvethen 发表于 2008-9-4 17:14

将char型转为float型

将char型转为float型
char b;
float a;
a=atof(b);
这样可以吗?

[[it] 本帖最后由 sdvethen 于 2008-9-4 17:17 编辑 [/it]]

QQ0001000 发表于 2008-9-4 20:23

函数名: atof
功  能: 把字符串转换成浮点数
用  法: double atof(const char *nptr);
所以不应该将一个字符变量当参数吧.

dtlcq 发表于 2008-9-4 20:36

#include <stdlib.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
        char b='2',*temp;
        float a;
        temp=&b;
    a = atof(temp);
        printf("%f",a);
        return 0;
}

josen0205 发表于 2008-9-5 16:15

a = atof((char *)&b);

StarWing83 发表于 2008-9-5 16:55

LS:你想程序崩溃么?

char想转float?
char c='2';

float f=c-'0';

页: [1]

编程论坛