The C atof() function converts text to double. Leading white space is skipped and a floating-point prefix is converted using rules similar to strtod(str, NULL). No useful error reporting is available.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. A valid representable prefix produces its double value. Conversion failure is not distinguishable reliably; use strtod() with endptr and errno. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete atof() call:
C
atof("3.75");
Call
What it does
atof("3.75");
Converts text to double using the concrete values shown.
Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.
Library declaration
C
double atof(const char *str);
Parameters
Parameter
Description
str
Null-terminated byte string containing a floating-point number.
Return Value
Return Value
Description
double value
The converted value.
0.0
Either a valid zero or an unreported conversion problem.
C Examples
Example 1 — Convert known text
This complete program demonstrates a distinct part of the atof() contract and checks the result or precondition that matters for the scenario.
Step 3. The function stops after the numeric prefix.
Step 4. Print the double result; validated input should use strtod instead.
Frequently Asked Questions
atof() converts text to double. Leading white space is skipped and a floating-point prefix is converted using rules similar to strtod(str, NULL). No useful error reporting is available.
Include <stdlib.h> before calling atof().
A valid representable prefix produces its double value. Conversion failure is not distinguishable reliably; use strtod() with endptr and errno.
atof() cannot distinguish invalid input from a valid zero and does not expose the stopping point. Prefer strtod() for untrusted or validated input.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.