C library functions -atof()

C Standard Library - <stdlib.h>

Description

C Library Functionsdouble atof(const char *str)Pass the parameterstrThe string pointed to is converted to a floating-point number (of type double).

Declaration

Below is the declaration of the atof() function.

double atof(const char *str)

Parameter

  • str-- The 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 is performed, it returns zero (0.0).

Example

The following example demonstrates the usage 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, float value = %f\n", str, val); strcpy(str, "example"); val = atof(str); printf("String value = %s, float value = %f\n", str, val); return(0); }

Let us compile and run the above program, which will produce the following result:

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

C Standard Library - <stdlib.h>

other extensions