[CentOS] Strange C programming problem

fredex fredex at fcshome.stoneham.ma.us
Sat Jul 14 14:19:19 UTC 2007

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:


but it actually produces this:


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.


---- Fred Smith -- fredex at fcshome.stoneham.ma.us -----------------------------
  "And he will be called Wonderful Counselor, Mighty God, Everlasting Father,
  Prince of Peace. Of the increase of his government there will be no end. He 
 will reign on David's throne and over his kingdom, establishing and upholding
      it with justice and righteousness from that time on and forever."
------------------------------- Isaiah 9:7 (niv) ------------------------------
-------------- 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/40dd6cc0/attachment.sig>

More information about the CentOS mailing list