目录

double atof(const char *str)

描述 (Description)

C库函数double atof(const char *str)将字符串参数str转换为浮点数(类型为double)。

声明 (Declaration)

以下是atof()函数的声明。

double atof(const char *str)

参数 (Parameters)

  • str - 这是具有浮点数表示的字符串。

返回值 (Return Value)

此函数将转换后的浮点数作为double值返回。 如果无法执行有效转换,则返回零(0.0)。

例子 (Example)

以下示例显示了atof()函数的用法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
   float val;
   char str[20];
   strcpy(str, "98993489");
   val = atof(str);
   printf("String value = %s, Float value = %f\n", str, val);
   strcpy(str, "iowiki.com");
   val = atof(str);
   printf("String value = %s, Float value = %f\n", str, val);
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果 -

String value = 98993489, Float value = 98993488.000000
String value = iowiki.com, Float value = 0.000000
↑回到顶部↑
WIKI教程 @2018