I've got this little program I wrote to test something, and it keeps giving the wrong result. I'm not inexperienced in C, but I can't believe strtof (et al) are broken, so I must be doing something wrong. However, I've spent hours looking at this and comparing it to the man pages and don't see what I'm doing wrong. strtod() and strtold() also give equally wrong results. (the example program given on the strotd man page works fine, BTW.)
Can someone wield a clue-bat please? :)
Here's the program:
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <errno.h>
int main (int argc, char ** argv) { float ldbl = 0.0; char * endp;
printf ("%s\n", argv[1]);
errno = 0; ldbl = strtof (argv[1], &endp); if (errno != 0) printf ("strtof failed! errno=%d\n", errno);
printf ("%f\n", (double) ldbl); printf ("%f\n", (double) strtof (argv[1], (char **)NULL)); printf ("%f\n", (double) atof (argv[1]));
return 0; }
Compile it with:
cc -O0 -g -o x4 x4.c
then run it like this:
./x4 2.5
and I'd EXPECT it to produce this output:
2.5 2.5 2.5 2.5
but it actually produces this:
2.5 1075838976.000000 1075838976.000000 2.500000
the typecase of the arg in the 3 printf calls makes no difference. Remove it and the results are the same.
Using an input of something other than 2.5 changes the middle two lines in some way in which I haven't yet discerned a pattern, but the result is still highly bogus.
Thanks!