What's wrong with using the atof function?

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

      

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

-0.371627

      

+3


source to share


2 answers


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

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

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

      



Without including stdlib.h, it is atof()

declared implicitly and the compiler assumes that it returns int.

+13


source


This may be undefined behavior .



+1


source







All Articles