Strange C programming problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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!

-- 
---- Fred Smith -- fredex@xxxxxxxxxxxxxxxxxxxxxx -----------------------------
  "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) ------------------------------

Attachment: pgpZ2LpB1WME8.pgp
Description: PGP signature

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

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux