Re: [CentOS] Strange C programming problem

2007-07-14 Thread Andy Green

> 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.

Adding -std=gnu99 to the compile makes it work, it seems by selecting a
different strtof implementation in /usr/include/stdlib.h.  Maybe the gcc
folks are interested, I dunno.

-Andy
___
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos


RE: [CentOS] Strange C programming problem

2007-07-14 Thread Michael Velez

 

> 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 
> #include 
> #include 
> #include 
> 
> 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.00
>   1075838976.00
>   2.50
> 
> 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

___
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos


Re: [CentOS] Strange C programming problem

2007-07-14 Thread fredex
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 
> > #include 
> > #include 
> > #include 
> > 
> > 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.00
> > 1075838976.00
> > 2.50
> > 
> > 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: [EMAIL PROTECTED] 
//  (__ (___ (__(_ (___ / :__ 781-438-5471 
 Jude 1:24,25 -


pgpNiPSHtvWEl.pgp
Description: PGP signature
___
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos