Easy Tutorial
❮ C Examples Digits Count C Standard Library Stddef H ❯

C Library Function - atof()

C Standard Library - <stdlib.h>

Description

The C library function double atof(const char *str) converts the string pointed to by the argument str into a floating-point number (type double).

Declaration

Here is the declaration for the atof() function.

double atof(const char *str)

Parameters

Return Value

The function returns the converted double value, and if no valid conversion could be performed, it returns zero (0.0).

Example

The following example demonstrates the use of the atof() function.

#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, "tutorialpro");
   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:

String value = 98993489, Floating-point value = 98993488.000000
String value = tutorialpro, Floating-point value = 0.000000

C Standard Library - <stdlib.h>

❮ C Examples Digits Count C Standard Library Stddef H ❯