Questions tagged [strtod]

strtod is the C standard library function for converting an ASCII string into a floating-point number.

strtod is the C standard library function (found in the ANSI standard, section 2.13.2.4 - found in stdlib.h) which converts a string into a floating point number.


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

  • ptr: A pointer to the string that will be converted
  • endptr: A pointer to where the last successful character match occurred.

    For example, calling strtod on the string "123abc" yields an endptr which is equal to ptr + 3. This is often used to test whether or not a string conversion went correctly - if no conversion took place, then *endptr == ptr.

    Note that this does not occur if endptr is NULL.


According to ANSI,

The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent.

(This effectively allows both "normal" decimal numbers, as well as scientific notation).

When the converter experiences either an overflow or underflow, errno is set to ERANGE, and either 0 (for underflows) or HUGE_VAL (for overflows) - it is thus recommended to clear errno before calling this function, otherwise an underflow error and the conversion from "0" are impossible to differentiate.

61 questions
-2
votes
1 answer

Parsing string to floats in C

I'm running a program that opens a .csv file that contains 3 different (float) values on each line. I want to separate the different values in different variables. For that I have already used strtod() funcion but it only works on Visual Studio and…
canibalimao
  • 27
  • 1
  • 1
  • 7
1 2 3 4
5