1

I want to know if parsing a string and converting the numerical string content to double value while writing a code in C will pose any accuracy problems?

If so, what kind of them?

Similarly are there any problems converting a double value to string?

If there's a problem, what is the best solution?

Thanks.

Smita
  • 11
  • 1
  • 1
    Possible duplicate of [C convert section of char array to double](http://stackoverflow.com/questions/15583965/c-convert-section-of-char-array-to-double) – Ani Menon Jul 15 '16 at 14:52

2 Answers2

1

atof will not pose accuracy problems; however, if the string represents an out-of-range double value, it will lead to undefined behavior (from this source).

sprintf similarly will yield accurate results as long as the buffer you are loading the data into is sufficiently large (sprintf).

Jack Ryan
  • 1,287
  • 12
  • 26
0

Consider also using strtod which seems to have better-defined behaviour for erroneous input, particularly in C11.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30