C++ Standard Library C++ STL Library

C++ <cstdlib> - atof() Function



The C++ <cstdlib> atof() function is used to interpret a floating-point value in a byte string pointed to by str.

The function first discards any whitespace characters (as determined by isspace()) until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.

The valid floating-point value for this function using the "C" locale is formed by an optional sign character (+ or -), followed by one of:

  • A sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).

  • Since C99 / C++11
    • A 0x or 0X prefix, then a sequence of hexadecimal digits (as in isxdigit()) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p or P character followed by an optional sign and a sequence of hexadecimal digits).
    • INF or INFINITY (ignoring case).
    • NAN or NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).

If the first sequence of non-whitespace characters in str is not a valid floating-point number, or if no such sequence exists (because either empty or contains only whitespaces), no conversion is performed and zero is returned.

Syntax

double atof (const char* str);

Parameters

str Specify pointer to the null-terminated byte string to be interpreted.

Return Value

On success, the function returns the converted floating-point number as a value of type double. If no conversion can be performed, 0.0 is returned. If the converted value falls out of the range, it causes undefined behavior.

Example:

The example below shows the usage of <cstdlib> atof() function.

#include <cstdio>
#include <cstdlib>

int main (){
  char str1[] = "123";
  char str2[] = "10.55";
  char str3[] = "100 some words";
  char str4[] = "some words 555";
  char str5[] = "inF";
  char str6[] = "Nan(2)";

  double num1 = atof(str1);
  double num2 = atof(str2);
  double num3 = atof(str3);
  double num4 = atof(str4);
  double num5 = atof(str5);
  double num6 = atof(str6);

  //displaying the result
  printf("atof(\"%s\") = %.2f\n", str1, num1);
  printf("atof(\"%s\") = %.2f\n", str2, num2);
  printf("atof(\"%s\") = %.2f\n", str3, num3);
  printf("atof(\"%s\") = %.2f\n", str4, num4);
  printf("atof(\"%s\") = %.2f\n", str5, num5);
  printf("atof(\"%s\") = %.2f\n", str6, num6);

  return 0;
}

The output of the above code will be:

atof("123") = 123.00
atof("10.55") = 10.55
atof("100 some words") = 100.00
atof("some words 555") = 0.00
atof("inF") = inf
atof("Nan(2)") = nan

❮ C++ <cstdlib> Library