What is wrong with usage of atof function?

血红的双手。 提交于 2020-01-03 18:37:27

问题


int main()
{
    char str[10]="3.5";
    printf("%lf",atof(str));
    return 0;
}

This is a simple code I am testing at ideone.com. I am getting the output as

-0.371627

回答1:


You have not included stdlib.h. Add proper includes:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[10]="3.5";
    printf("%lf",atof(str));
    return 0;
}

Without including stdlib.h, atof() is declare implicitly and the compiler assumes it returns an int.




回答2:


It could be undefined behavior.



来源:https://stackoverflow.com/questions/14918189/what-is-wrong-with-usage-of-atof-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!