C Programming/stdlib.h/atof

From Wikibooks, open books for an open world
Jump to navigation Jump to search

atof is a function in the C programming language that converts a string into a floating point numerical representation. atof stands for ASCII to float. It is included in the C standard library header file stdlib.h. Its prototype is as follows:

double atof (const char *str);

The str argument points to a string, represented by an array of characters, containing the character representation of a floating point value. If the string is not a valid textual representation of a double, atof will silently fail, returning zero (0.0) in that case. [1]

Note that while atoi and atol return variable types corresponding with their name ("atoi" returns an integer and "atol" returns a long integer), atof however, does not return a float, it returns a double.

A related function is sscanf. This function extracts values from strings and its return argument is the number of valid values it managed to extract (so, unlike atof, sscanf can be used to test if a string starts with a valid number).

References[edit | edit source]

  1. ISO/IEC 9899:1999 specification, p. 307, § 7.20.1.1

External links[edit | edit source]