C library - strtod() function



The C stdlib library strtod() function is used to convert a string pointed to by the 'str" to a double precision floating-point number. If 'endptr' is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by 'endptr'.

Note: The strtod() function provides greater precision and can handle a wider range of values compared to atof() function.

Syntax

Following is the C library syntax of the strtod() function −

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

Parameters

This function accepts following parameters −

  • str − It is a string to be converted into a double precision floating-point number.

  • endptr − It is a pointer to a charter pointer and used to store the pointer to the first character after the numeric value.

Return Value

This function returns the converted double-precision floating-point number. If the input string is not a valid, it returns 0.

Example 1

In this example, we are converting the string to a floating-point number and extracting the charter after numeric value using the strtod() function.

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

int main () { 
   char str[50] = "2024.05 tutorialspoint";
   char *ptr;
   double res;
   
   // convert into floating point number
   res = strtod(str, &ptr);
   
   //display the numeric part
   printf("Double value: %f\n", res);
   //display the string after number
   printf("String after number: %s", ptr);
   
   return(0);
}

Output

Following is the output −

Double value: 2024.050000
String after number:  tutorialspoint

Example 2

Let's create another example, we concatenated two string and then convert the resulting string to a floating-point number and extracting the character after number, using the strtod() function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {	
   // Define two strings to concatenate
   char str1[] = "2024.05 ";
   char str2[] = "tutorialspoint";
   char *ptr;
   
   //calculate the length of string first + second
   int length = strlen(str1) + strlen(str2) + 1;

   // Allocate memory for the concatenated string
   char *concatenated = malloc(length);

   // check memory allocation if null return 1.
   if(concatenated == NULL) {
      printf("Memory allocation failed\n");
      return 1;
   }

   // Concatenate str1 and str2
   strcpy(concatenated, str1);
   strcat(concatenated, str2);

   // use the strlen() function
   double number = strtod(concatenated, &ptr);

   printf("The concatenated string is: %s\n", concatenated);
   printf("The double value is: %f\n", number);
   printf("String after number: %s", ptr);

   // at the last free the alocated memory
   free(concatenated);
   return 0;
}

Output

Following is the output −

The concatenated string is: 2024.05 tutorialspoint
The double value is: 2024.050000
String after number:  tutorialspoint

Example 3

The example below convert the character string into the double precision floating-point number, using the strtod() function.

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

int main () {
   double res;
   char str[20];
   char *ptr;
   //define a string
   strcpy(str, "tutorialspoint");
   
   //use strtod() function
   res = strtod(str, &ptr);
   printf("String Value = %s\n", str);
   printf("Double value = %f\n", res);
   printf("Charater after number = %s", ptr);

   return(0);
}

Output

Following is the output, Here we are getting double value as '0' because string value is not a valid floating point string.

String Value = tutorialspoint
Double value = 0.000000
Charater after number = tutorialspoint
Advertisements