C library function - atof()
Description
C library function *double atof(const char str) Converts the string pointed to by the argument str into a floating-point number (of type double).
Declaration
The following is the declaration of the atof() function.
double atof(const char *str)
Parameters
- str -- String to be converted to a floating-point number.
Return Value
The function returns the converted double-precision floating-point number; if no valid conversion was performed, it returns zero (0.0).
Example
The following example demonstrates the use of the atof() function.
Example
#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, floating-point value = %f
\
n
"
,
str
,
val
)
;
strcpy
(
str
,
"
runoob
"
)
;
val
=
atof
(
str
)
;
printf
(
"
String value = %s, floating-point value = %f
\
n
"
,
str
,
val
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
字符串值 = 98993489, 浮点值 = 98993488.000000
字符串值 = runoob, 浮点值 = 0.000000
C Standard Library -