Use atof function to convert string to double

Syntax

C atof function has the following syntax.

double atof(const char *str);

C atof function is from header file stdlib.h.

Description

C atof function converts the string into a double value.

Example

Use C atof function to convert string to double.


#include <stdlib.h>
#include <stdio.h>
int main(void)
{/*ww  w .j  a v a 2  s  .c om*/
  char num1[80], num2[80];

  printf("Enter first: ");
  gets(num1);
  printf("Enter second: ");
  gets(num2);
  printf("The sum is: %lf.", atof(num1) + atof(num2));

  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h