atof() Function

The atof() function in C converts a string into a double-precision floating point number. It interprets the beginning of the string as a floating point value and returns the corresponding double. If the string does not start with a valid number, the function returns 0.0.


Syntax of atof()

</>
Copy
double atof(const char *str);

Parameters

ParameterDescription
strA C-string beginning with the representation of a floating-point number.

Return Value

The function returns the converted value as a double. On failure, it returns 0.0. Undefined behavior may occur if the converted value is out of the representable range for a double.

Notes

atof() first skips any leading whitespace characters. It then processes as many characters as possible that form a valid floating-point number. Any characters following the valid number are ignored. For more robust error handling in cases where the value might exceed the representable range of a double, consider using strtod().


Examples for atof()

Example 1: Converting a Valid Floating-Point String

This example demonstrates how to convert a string that contains a valid floating-point number using atof().

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "123.456";
    double num = atof(str);
    printf("Converted number: %f\n", num);
    return 0;
}

Explanation:

  1. The string "123.456" is provided to atof().
  2. atof() converts the valid floating-point representation to the double value 123.456.
  3. The resulting number is printed to the console.

Program Output:

Converted number: 123.456000

Example 2: Converting a String with Leading Whitespace and Trailing Characters

This example shows how atof() handles a string with leading whitespace and extra non-numeric characters. The function skips the whitespace and stops parsing when it encounters characters that are not part of a valid floating-point number.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "   -78.90abc";
    double num = atof(str);
    printf("Converted number: %f\n", num);
    return 0;
}

Explanation:

  1. Leading whitespace in the string is ignored.
  2. The function converts the valid numeric part -78.90 and stops when it reaches the non-numeric characters.
  3. The resulting double value is then printed.

Program Output:

Converted number: -78.900000

Example 3: Handling an Invalid Numeric String Resulting in Zero

This example illustrates the behavior when the input string does not begin with a valid numeric representation. In such cases, atof() returns 0.0.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "hello";
    double num = atof(str);
    printf("Converted number: %f\n", num);
    return 0;
}

Explanation:

  1. The string "hello" does not represent a valid floating-point number.
  2. As a result, atof() returns 0.0.
  3. This output is then printed to the console.

Program Output:

Converted number: 0.000000