How can I check if a string can be converted to a float?

前端 未结 7 1438
别那么骄傲
别那么骄傲 2021-01-15 04:38

First my context is that of a compiler writer who needs to convert floating point literals (strings) into float/double values. I haven\'t done any floating point programming

7条回答
  •  有刺的猬
    2021-01-15 04:54

    In C89 you may use sscanf.

    For example:

    float myfloat;
    
    if(sscanf(str, "%f", &myfloat) != 1)
        /* String could not be converted */
    else
        /* String was converted, myfloat is usable */
    

提交回复
热议问题