C++

What is strtod() Function in C++?

If you’re working on C++ programming, chances are you have come across situations where you need to convert strings to floating-point numbers. One function that can help you do that is strtod() which parses a string as a double-precision floating-point value and returns its numerical value. However, strtod() can be tricky to use, especially when dealing with non-standard formats and locales.

This article will go over strtod() in depth, including its syntax and applications in C++.

What is the strtod() Function?

The strtod() function in C++ is a powerful and useful tool for converting a string of characters into a floating-point value. The function essentially takes a C-string as its argument and returns the corresponding floating-point value. In C++, strtod() is a part of the standard library and is typically used for more complex numerical data processing tasks.

The strtod() function is defined in the header file <cstdlib> and is included in the C++ standard library. It is a fundamental part of any programming language as it provides the capability of inputting numerical values through strings, which are more convenient and user-friendly.

The syntax of the strtod() function is:

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

It takes two arguments – the first being the string input and the second being a pointer to a character string that remains after the conversion.

Return Value of strtod() Function

The function returns a floating-point number value if the conversion is successful and zero if an error occurs.

Uses of strtod() Function

Some of the uses of strtod() function are:

1: Convert a String to a Floating-Point Number

Here’s how you use C++’s strtod() function to convert a string to a double-precision floating-point number:

#include <cstdlib>
#include <iostream>

int main() {
    char str[] = "3.14159";
    char* endptr;

    double num = strtod(str, &endptr);

    std::cout << "The number is: " << num << std::endl;
    std::cout << "The first invalid character is: " << *endptr << std::endl;

    return 0;
}

In this example, we declare a character array called str with the string 3.14159 in it. We then make a character pointer named endptr. Both of these inputs are passed to the strtod() method, which converts 3.14159 to the double-precision floating-point number 3.14159. Using std::cout, the value of num is then printed to the console.

Using the *endptr reference, we additionally print the value of the first incorrect character. In this situation, the value of *endptr is the null character (‘0’), because the full string 3.14159 is converted to a double-precision floating-point number.

Output

Let’s add an invalid character in the str variable:

#include <cstdlib>
#include <iostream>

int main() {
    char str[] = "3.141a59";
    char* endptr;

    double num = strtod(str, &endptr);

    std::cout << "The number is: " << num << std::endl;
    std::cout << "The first invalid character is: " << *endptr << std::endl;

    return 0;
}

Output

In the above example, character a is added to the string variable.

2: Detect Errors

The strtod() function only translates the first part of the string that represents a valid floating-point value. If the string contains any incorrect characters, the function will stop converting and return the value it has so far transformed. The *endptr reference indicates where the function stopped converting, allowing the calling function to assess whether the entire string was successfully converted.

Here’s an example of how you can utilize the *endptr pointer to detect errors:

#include <cstdlib>
#include <iostream>

int main() {
    char str[] = "3.14159x";
    char* endptr;

    double num = strtod(str, &endptr);

    if (*endptr != '\0') {
        std::cerr << "Invalid character found: " << *endptr << std::endl;
    }
    else {
        std::cout << "The number is: " << num << std::endl;
    }

    return 0;
}

In the above code, we have appended an invalid character x to the end of the string 3.14159. When you call the strtod() function, it stops converting at the character x and returns the number 3.14159. The character x is then assigned to the value of *endptr. To test whether the complete string was converted, we look at the value of *endptr. We output an error message saying that an incorrect character was found since the value of *endptr is not the null character (‘0’).

Output

3: Convert a Scientific Notation to Floating Point

The strtod() function can be used to convert various string representations of floating-point numbers such as scientific notation, hexadecimal notation, and signed notation. When dealing with hex digits, it can accept both positive and negative numbers and is case-insensitive. The power of ten is denoted by the letter e or E preceded by an exponent.

Here’s an example of how to use the strtod() function to convert a scientific notation string to a double-precision floating-point number:

#include <cstdlib>
#include <iostream>

int main() {
    char str[] = "0.000043";
    char* endptr;
    double num = strtod(str, &endptr);
    std::cout << "The number is: " << num << std::endl;
    std::cout << "The first invalid character is: " << *endptr << std::endl;

    return 0;
}

In the above code, we initialize a character array called str with the string 0.000043 in it. The strtod() function is then called with the inputs str and &endptr. The function transforms the string to the scientific notation for the double-precision floating-point value. Using std::cout, the value of num is then printed to the console.

Output

It’s worth noting that the strtod() function only recognizes the letters e and E as scientific notation. If the string contains any additional characters, the function will stop converting and return the value transformed thus far.

Conclusion

The strtod() function is a vital tool in any programmer’s arsenal, and its applications are widespread in various areas requiring numerical inputs. It provides a robust way of handling string to floating-point number conversions while ensuring proper error handling, and its support for different number formats makes it a versatile solution for solving numerical problems.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.