strtod - C stdlib.h

C examples for stdlib.h:strtod

type

function

From


<cstdlib>
<stdlib.h>

Description

Convert string to double

Prototype

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

Parameters

Parameter Description
str C-string representation of a floating-point number.
endptr an char* to the next character in str after the numerical value. This parameter can be a null pointer if it is not used.

Return Value

On success, the function returns the converted floating point number as a value of type double.

For non valid conversion, the function returns 0.0.

For out of the range values, a positive or negative HUGE_VAL is returned, and errno is set to ERANGE.

Demo Code


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

int main ()/*from  ww w .j  a va2 s  .  com*/
{
  char str[] = "1234.1234 123.53 test";
  char* pEnd;
  
  double d1, d2;
  
  d1 = strtod (str, &pEnd);
  
  
  d2 = strtod (pEnd, NULL);
  
  printf ("%f\n", d1);
  printf ("%f\n", d2);
  
  return 0;
}

Related Tutorials