strtod() — strtof() — strtold — Convert Character String to Double, Float, and Long Double

Format

#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of these functions might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The strtod(), strtof(), and strtold() functions convert a character string to a double, float, or long double value. The parameter nptr points to a sequence of characters that can be interpreted as a numeric binary floating-point value. These functions stop reading the string at the first character that is not recognized as part of a number. This character can be the null character at the end of the string.

The strtod(), strtof(), and strtold() functions expect nptr to point to a string with the following form:

Read syntax diagramSkip visual syntax diagram>>-+------------+--+-----+--+-digits--+---+--+--------+-+------->
   '-whitespace-'  +- + -+  |         '-.-'  '-digits-' |
                   '- – -'  '-.--digits-----------------'
 
>--+------------------------+----------------------------------><
   '-+-e-+--+-----+--digits-'
     '-E-'  +- + -+
            '- – -'
 

The first character that does not fit this form stops the scan. In addition, a sequence of INFINITY or NAN (ignoring case) is allowed.

Return Value

The strtod(), strtof(), and strtold() functions return the value of the floating-point number, except when the representation causes an underflow or overflow. For an overflow, strtof() returns HUGE_VALF or -HUGE_VALF; strtod() and strtold() return HUGE_VAL or -HUGE_VAL. For an underflow, all functions return 0.

In both cases, errno is set to ERANGE. If the string pointed to by nptr does not have the expected form, no conversion is performed and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

The strtod(), strtof(), and strtold() functions do not fail if a character other than a digit follows an E or e that is read as an exponent. For example, 100elf is converted to the floating-point value 100.0.

A character sequence of INFINITY (ignoring case) yields a value of INFINITY. A character value of NAN yields a Quiet Not-A-Number (NAN) value.

Example that uses strtod(), strtof(), and strtold()

This example converts the strings to double, float, and long double values. It prints the converted values and the substring that stopped the conversion.

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   char *string, *stopstring;
   double x;
   float f;
   long double ld;

   string = "3.1415926This stopped it";
   f = strtof(string, &stopstring);
		printf("string = %s\n", string);
		printf("		strtof = %f\n", f);
		printf("Stopped scan at \"%s\"\n\n", stopstring);

		string = "3.1415926This stopped it";
   x = strtod(string, &stopstring);
   printf("string = %s\n", string);
   printf("   strtod = %f\n", x);
   printf("   Stopped scan at %s\n\n", stopstring);
   string = "100ergs";
   x = strtod(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtod = %f\n", x);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);

		string = "3.1415926This stopped it";
   ld = strtold(string, &stopstring);
   printf("string = %s\n", string);
   printf("   strtold = %lf\n", ld);
   printf("   Stopped scan at %s\n\n", stopstring);
   string = "100ergs";
   ld = strtold(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtold = %lf\n", ld);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);
}

/*****************  Output should be similar to:  *****************
string = 3.1415926This stopped it
   strtof = 3.141593
   Stopped scan at This stopped it

string = "100ergs
   strtof = 100.000000
   Stopped scan at "erg

string = 3.1415926This stopped it
   strtod = 3.141593
   Stopped scan at This stopped it
 
string = "100ergs
   strtod = 100.000000
   Stopped scan at "erg

string = 3.1415926This stopped it
   strtold = 3.141593
   Stopped scan at This stopped it
 
string = "100ergs
   strtold = 100.000000
   Stopped scan at "erg

*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]