Pages

2011-10-28

strtod and atof

#include <stdio.h>

int main(int argc, const char *argv[])
{
        double d = strtod("10.04", NULL);
        double e = atof("200.04");
        printf("%f\n", d);
        printf("%f\n", e);
        return 0;
}

% gcc b.c -Wall
b.c: In function ‘main’:
b.c:33:9: warning: implicit declaration of function ‘strtod’
b.c:34:9: warning: implicit declaration of function ‘atof’
% ./a.out
267386.000000
590151.000000

It's using corresponding ones in gcc, not glibc, but the result is misleading.
Then add
#include <stdlib.h>

it's correct.
% ./a.out
10.040000
200.040000

No comments:

Post a Comment