Smita
Smita

Reputation: 11

Converting a string to double value in C

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.

Upvotes: 1

Views: 71

Answers (2)

LordWilmore
LordWilmore

Reputation: 2912

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

Upvotes: 0

Jack Ryan
Jack Ryan

Reputation: 1318

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).

Upvotes: 1

Related Questions