Table of Content

Atof : String to Float >> Math.h Function >> Header File in C

Atof :

  1. atof : Interpret it as [A to F] = Alphabet to Float
  2. atof converts a String to a floating point
  3. atof converts the string *s to double.
  4. Header File : Math.h

Which String Format can be Converted  into Double / Float ?

  1. An Optional String of Tabs and Spaces
  2. An Optional Sign
  3. A string of digits and an optional decimal point i.e [ digits on both sides of the decimal point ]
  4. An optional e or E followed by an optional signed integer

Atof Also Recognize ?

  1. +INF and -INF (plus and minus infinity)
  2. +NAN and -NAN (Not-a-Number)

Syntax :

double atof(const char *s);

Live Example :


int main(void)
float f;
char *str = "12345.67";

f = atof(str);
printf("string = %s float = %fn", str, f);
return 0;

Note :

  1. In atof, the first unrecognized character ends the conversion.
  2. On success, these functions return the converted value of the input string.
  3.  If there is an overflow, atof and _atold:
      ■ return plus or minus HUGE_VAL
      ■ set errno to ERANGE
      ■ do NOT call matherr or _matherrl