C++ atof()

atof() prototype

double atof(const char* str);

It is defined in <cstdlib> header file.


atof() Parameters


atof() Return value

The atof() function returns:

  • a double value (which is converted from the string).
  • 0.0 if no valid conversion could be performed.

If the converted value is out of the range, it causes undefined behavior.


Example 1: How atof() function works?

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	char numberString[] = "-32.40"; 
	double numberInDouble;
	cout << "Number in  String = " << numberString << endl;

	numberInDouble = atof(numberString);
	cout << "Number in Double = " << numberInDouble;
	
	return 0;
}

When you run the program, the output will be:

Number in  String = -32.40
Number in Double = -32.4

A valid floating-point value for atof() function consists of an optional + or - sign followed by one of the following sets:

  • For decimal floating-point value:
    • A group of decimal digits (0-9), optionally containing a decimal point (.). For example: 9.056, -0.013, etc.
    • An optional exponent part (e or E) followed by an optional + or - sign and non-empty sequence of decimal digits. For example: 1.23455e+009, 5.23e-018, etc.
  • For hexadecimal floating-point value:
    • A string starting with 0x or 0X, followed by a non-empty sequence of hexadecimal digits, optionally containing a decimal point (.). For Example: 0xf1b, -0xb1b.51, etc.
    • An optional exponent part (p or P) followed by an optional + or - sign and non-empty sequence of hexadecimal digits. For Example: 0x51c.23p5, -0x2a.3p-3, etc.
  • Infinity:
    • INF or INFINITY (ignoring case). For Example: -iNf, INfINiTy, etc.
  • NaN (Not a Number):
    • NAN or NANsequence (ignoring case) where sequence is a sequence of characters consisting only of alphanumeric characters or the underscore (_).The result is a quiet NaN. For Example: Nan, NaN12, etc.

Example 2: How atof() works with exponents and hexadecimals?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

	cout << "-44.01e-3" << " to Double = " << atof("-44.01e-0") << endl;
	cout << "-44.01e-3" << " to Double = " << atof("-44.01e-3") << endl;
	
	cout << "0xf1bc" << " to Double = " << atof("0xf1bc") << endl;
	cout << "0xf1bc.51" << " to Double = " << atof("0xf1bc.51") << endl;
	
	return 0;
}

When you run the program, the output will be:

-44.01e-3 to Double = -44.01
-44.01e-3 to Double = -0.04401
0xf1bc to Double = 61884
0xf1bc.51 to Double = 61884.3

Example 3: atof Cases for INFINITY and NaN

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	cout << "INFINITY" << " to Double = " << atof("INFINITY") << endl;
	cout << "Inf" << " to Double = " << atof("Inf") << endl;

	cout << "Nan" << " to Double = " << atof("Nan") << endl;
	cout << "NAN" << " to Double = " << atof("NAN") << endl;
	
	return 0;
}

When you run the program, the output will be:

INFINITY to Double = inf
Inf to Double = inf
Nan to Double = nan
NAN to Double = nan

In general, a valid floating point argument for atof() function has the following form:

[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]

The atof() function ignores all the leading whitespace characters until the primary non-whitespace character is found.

Then, beginning from this character, it takes as many characters as possible that forms a valid floating-point representation and converts them to a floating point value. Whatever is left of the string after the last valid character is ignored and has no effect on the result.


Example 4: atof() function with Whitespace and trailing characters

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    cout << "25.5" << " to Double = " << atof("  25.5") << endl;
    cout << "25.5    " << " to Double = " << atof("  25.5   ") << endl;
    cout << "25.5abcd" << " to Double = " << atof("25.5abcd") << endl;

    // Returns 0 because of invalid conversion
    cout << "abcd25.5" << " to Double = " << atof("abcd25.5") << endl;

    // Rules for whitespace and trailing character also apply for infinity and Nan
    cout << "INFINITYabcd" << " to Double = " << atof("INFINITYabcd") << endl;
    cout << "INFINITY" << " to Double = " << atof("  INFINITY") << endl;
    cout << "Nanlll" << " to Double = " << atof("Nanlll") << endl;

    return 0;
}

When you run the program, the output will be:

25.5 to Double = 25.5
25.5     to Double = 25.5
25.5abcd to Double = 25.5
abcd25.5 to Double = 0
INFINITYabcd to Double = inf
INFINITY to Double = inf
Nanlll to Double = nan
Did you find this article helpful?