5
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
Naved Alam
  • 827
  • 9
  • 25

2 Answers2

17

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
0

It could be undefined behavior.

Community
  • 1
  • 1
austin
  • 5,816
  • 2
  • 32
  • 40
  • no, `%lf` is the correct specifier for double, there's no issue with that. It's just that [*"If the converted value falls out of range of the return type, the return value is undefined"*](https://en.cppreference.com/w/c/string/byte/atof) which is the same reason why [`atoi()` shouldn't be used](https://stackoverflow.com/q/17710018/995714) – phuclv Dec 22 '20 at 00:50