Function that reads a double from a string and returns the remainder of the string in C

1

Is there any function of the type? Which reads and stores or returns a double of a string and returns or passes the rest of the string pointer or will I have to do my own function? If no one has ideas how to do this function optimally?

    
asked by anonymous 03.10.2016 / 21:55

1 answer

1

There is a function:

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

It converts a number double from the string passed as the first parameter, and plays a pointer to the rest of the string in the second parameter. Example:

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

int main ()
{
  char texto[] = "1.21 gigawatts";
  char* resto;
  double num;
  num = strtod (texto, &resto);
  printf ("Num: %f\n", num);
  printf ("Resto: %s\n", resto);
  return 0;
}

Output:

Num: 1.210000
Resto:  gigawatts
    
03.10.2016 / 22:11