strtod() function in C/C++

CC++Server Side Programming

The function strtod() is used to convert the string to a floating point number. The string is converted into double type number. It returns the converted number, if successful otherwise, zero. This is declared in “stdlib.h” header file.

Here is the syntax of strtod() in C language,

double strtod(const char *string, char **endpointer);

Here,

string − The string to be converted.

endpointer − The pointer of already allocated object and its value is set to the next character by the function after the numeric value.

Here is an example of strtod() in C language,

Example

 Live Demo

#include <stdio.h>
#include <stdlib.h>
int main () {
   char s[20] = "8.28 is a number";
   char *p;
   double result;
   result = strtod(s, &p);
   printf("The number after conversion of string : %lf", result);
   return(0);
}

Output

The number after conversion of string : 8.280000

In the above program, a char type array s[20] is declared which is initialized with a alphanumeric characters. The function strtod() is used to convert that string into a double number.

char s[20] = "8.28 is a number";
char *p;
double result;
result = strtod(s, &p);
raja
Published on 05-Nov-2018 11:06:03
Advertisements