strtod(), strtof(), strtold()

QNX SDP8.0C Library ReferenceAPIDeveloper

Convert a string into a double-precision number

Synopsis:

#include <stdlib.h>

double strtod( const char *ptr, 
               char **endptr );

float strtof( const char *ptr,
              char **endptr );

long double strtold( const char *ptr,
                     char **endptr );

Arguments:

ptr
A pointer to the string to parse.
endptr
If this argument isn't NULL, the function stores in it a pointer to the first unrecognized character found in the string.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The strtod(), strtof(), and strtold() functions convert the string pointed to by ptr into a double-precision representation:

This function: Returns:
strtod() double
strtof() float
strtold() long double

These functions skip any leading white space, and then look for a subject sequence that consists of an optional plus or minus sign followed by one of the following:

  • a non-empty sequence of decimal digits optionally containing a radix character, then an optional exponent part
  • 0x or 0X, then a non-empty sequence of hexadecimal digits optionally containing a radix character, then an optional binary exponent part
  • INF or INFINITY, ignoring case
  • NAN, ignoring case
    Note:
    POSIX says that these functions can optionally parse an n-char sequence after the NAN. The QNX OS versions don't interpret an n-char sequence, and the result is equivalent to specifying NAN without the n-char sequence.

The conversion ends at the first unrecognized character. If endptr isn't NULL, a pointer to the unrecognized character is stored in the object endptr points to.

Returns:

The converted value. If the correct value would cause overflow, plus or minus HUGE_VAL is returned according to the sign, and errno is set to ERANGE. If the correct value would cause underflow, a value with a magnitude no greater than the smallest normalized positive number in the return type is returned and errno is set to ERANGE.

Note:
These functions set errno if the input string can't be converted; they don't change errno if no errors occurred. If you want to check for errors, set errno to 0, call the function, and then check errno again.

Errors:

ERANGE
The value to be returned would cause overflow or underflow.
EINVAL
No conversion could be performed.

Examples:

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

int main( void )
  {
    double pi;

    pi = strtod( "3.141592653589793", NULL );
    printf( "pi=%17.15f\n",pi );
    return EXIT_SUCCESS;
  }

Classification:

ANSI, POSIX 1003.1

Safety:
Cancellation pointNo
Signal handlerYes
ThreadYes
Page updated: