strtod function(Convert String to Double) in C Language – example and detail

In the C Programming Language, the strtod feature converts a string to a double.

The strtod feature skips all white-space characters at the commencing of the string, converts the subsequent characters as section of the number, and then stops when it encounters the first personality that isn’t a number.

Syntax

The syntax for the strtod function in the C Language is:

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

Parameters or Arguments

nptr A pointer to a string to convert to a double. endptr It is used by means of the strtod feature to indicate where the conversion stopped. The strtod feature will regulate endptr (if endptr is now not a null pointer) so that endptr factors to the first personality that used to be not converted.

Returns

The strtod function returns the double illustration of a string. The strtod characteristic skips all white-space characters at the starting of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that is not a number.

If the strtod function converts a price that is too massive or too small to convert, it will store ERANGE in errono.

Required Header

In the C Language, the required header for the strtod characteristic is:

#include <stdlib.h>

Applies To

In the C Language, the strtod function can be used in the following versions:

ANSI/ISO 9899-1990

strtod Example

Let’s seem at an example to see how you would use the strtod characteristic in a C program:

/* Example using strtod by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    char value[10];
    char *eptr;
    double result;

    /* Copy a value into the variable */
    /* It's okay to have whitespace before the number */
    strcpy(value, " 958");

    /* Convert the provided value to a double */
    result = strtod(value, &eptr);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%f decimal\n", result);

    /* Copy a hexadecimal value into the variable */
    strcpy(value, "0x8b2");

    /* Convert the hexadecimal provided value to a double */
    result = strtod(value, &eptr);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%f decimal\n", result);

    return 0;
}

When compiled and run, this software will output:

958.000000 decimal
2226.000000 decimal

Similar Functions

Other C functions that are similar to the strtod function:

atof function strtol function strtoul feature

See Also

Other C features that are noteworthy when dealing with the strtod function:

atoi function <stdlib.h>

atol function <stdlib.h>