当前位置: 动力学知识库 > 问答 > 编程问答 >

c - atof() for float instead of double

问题描述:

atof() returns a double, which results in a warning when I assign it to a float-value (and yes, I definitively have to use float).

So my question: is there a atof()-variant available which returns a plain float? Or do I have to solve this by a cast - which would be a pity because it wastes resources for creating a double which will be thrown away immediately.

网友答案:

Use strtof instead, it will return a float.

See documentation here

网友答案:

If the platform does have a direct "string to float" conversion routine and the compiler is optimizing, it will certainly call that conversion routine via:

char *buf = ...
float y; 
if (sscanf(buf, "%f", &y) == 1) Success();

If not, then use

float y = (float)atof(buf);

Note that the second could be optimized by the compiler to call the "string to float" if it violates the subtle double rounding that otherwise occurs with this 2nd code.

分享给朋友:
您可能感兴趣的文章:
随机阅读: