C 库函数 - atof()

C 语言教程 · 2019-02-23 23:42:13

描述

C 库函数 double atof(const char *str) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。

声明

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

double atof(const char *str)

参数

  • str -- 要转换为浮点数的字符串。

返回值

函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。

实例

下面的实例演示了 atof() 函数的用法。

实例

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { float val; char str[20]; strcpy(str, "98993489"); val = atof(str); printf("字符串值 = %s, 浮点值 = %f\n", str, val); strcpy(str, "codercto"); val = atof(str); printf("字符串值 = %s, 浮点值 = %f\n", str, val); return(0); }

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

字符串值 = 98993489, 浮点值 = 98993488.000000
字符串值 = codercto, 浮点值 = 0.000000

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

The Art of Computer Programming, Volumes 1-3 Boxed Set

The Art of Computer Programming, Volumes 1-3 Boxed Set

Donald E. Knuth / Addison-Wesley Professional / 1998-10-15 / USD 199.99

This multivolume work is widely recognized as the definitive description of classical computer science. The first three volumes have for decades been an invaluable resource in programming theory and p......一起来看看 《The Art of Computer Programming, Volumes 1-3 Boxed Set》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具