What is wrong with usage of atof function?

Naved Alam picture Naved Alam · Feb 17, 2013 · Viewed 7.9k times · Source
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

Answer

FatalError picture FatalError · Feb 17, 2013

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.