在C语言中,可以使用库函数'atof'将字符串转换为浮点数。

'atof'函数的原型如下:

double atof(const char *str);

其中,'str'为要转换的字符串,函数将返回转换后的浮点数。

以下是一个示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "3.14";
    double num = atof(str);
    printf("转换后的浮点数为:%f\n", num);
    return 0;
}

输出结果为:

转换后的浮点数为:3.140000

需要注意的是,如果字符串无法转换为浮点数,'atof'函数将返回0.0。因此,在使用'atof'函数时,需要进行错误处理。

C语言字符串转浮点数:atof函数详解

原文地址: https://www.cveoy.top/t/topic/fx8l 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录