Naved Alam
Naved Alam

Reputation: 837

What is wrong with usage of atof function?

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

Upvotes: 5

Views: 8276

Answers (2)

FatalError
FatalError

Reputation: 54591

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.

Upvotes: 17

austin
austin

Reputation: 5876

It could be undefined behavior.

Upvotes: 0

Related Questions