In C atof
=a
-to
-f
(loat) converts a string into a double precision float. I am wondering what the a
part of atof
stand for.

- 117
- 1
- 4
-
6ASCII to floating point. From [Farlex](https://acronyms.thefreedictionary.com/ATOF). – Weather Vane May 13 '22 at 16:40
-
4Unrelated: prefer `strtod()` rather than `atof()` if you care about validation and error checking and recovery. – pmg May 13 '22 at 16:42
-
2@zmkm I am not sure but it seems a stands for alpha. – Vlad from Moscow May 13 '22 at 16:43
-
1@WeatherVane I wonder why they chose such a name, because it is supposed to work with non-ascii encodings too. I guess just historical reasons. – Eugene Sh. May 13 '22 at 16:44
-
@VladfromMoscow there *is* one alpha in `"1.23e4"` but mostly digits. – Weather Vane May 13 '22 at 16:58
-
1"In C `atof()` means I've chosen the wrong function for the job". Why? `atoX()` functions have no error reporting and will silently fail returning `0` for something as wrong as `atof ("my cow");`. `strtof()` or `strtod()` provide full error reporting. – David C. Rankin May 13 '22 at 17:41
2 Answers
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).
-
5
-
3@WeatherVane: I'm not sure about the 'might' — something more definitive like 'is' or 'would' applies. But I agree that `strtod()` is better for validation. – Jonathan Leffler May 13 '22 at 16:51
-
Agree with both, but at least `sscanf()` is one-step up from `atof()`. It at least provides a *succeed / fail* indication of whether the conversion took place (assuming you ***check the return...***) – David C. Rankin May 13 '22 at 17:45
-
@DavidC.Rankin Note that overflow with `sscanf()` is UB - although I have never seen such UB that was evil: at worse a nonsense value was saved. – chux - Reinstate Monica May 13 '22 at 17:58
To best answer what the a
stands for, go back to early 1970s when bytes cost approached dollars each.
Even if a
originally stood for ASCII, atof()
did not and still does not mean to convert ASCII into double
as the implementation may have used an alternate character encoding. With EBCDIC or PETSCII, one could think of a
as alpha and write code for atof()
per that non-ASCII encoding.

- 143,097
- 13
- 135
- 256