On Sat, Jul 14, 2007 at 12:52:09PM -0400, Michael Velez wrote: > > > > > 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. > > > > The following strtod line works fine on my system (CentOS 5, latest updates, > x86_64): > > printf ("%lf\n", (double) strtod (argv[1], (char **)NULL)); > > For strtof, the SYNOPSIS in the man page mentions you need to add: > > #define _ISO_C99_SOURCE > > Or > > #define _XOPEN_SOURCE 600 > > Either line should be added before ALL include files (note there is a > mistake in the synopsis. There should be no = sign in the define statement > for _XOPEN_SOURCE). > > The above #define lines enforces C99 compatibility rules, which is the > revised ISO C standard which came out in 1999. As a previous responder > suggested, you can also specify -std=gnu99 or -std=C99 on the compile line. > > Michael Sorry, I forgot to mention that I'm using Centos 4.5. And the man page here doesn't mention those #define settings. I'll give it a try, thanks! -- ------------------------------------------------------------------------------- .---- Fred Smith / ( /__ ,__. __ __ / __ : / / / / /__) / / /__) .+' Home: fredex at fcshome.stoneham.ma.us / / (__ (___ (__(_ (___ / :__ 781-438-5471 -------------------------------- Jude 1:24,25 --------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: <http://lists.centos.org/pipermail/centos/attachments/20070714/bbcec75c/attachment-0005.sig>