David C. Rankin
2008-05-04 01:19:29 UTC
Listmates,
I was comparing input handling between perl and c and I ran into a very weird
problem with the strtof function in c. What am I doing wrong? strtof doesn't
seem to be working like the man page shows. Here is my test bit of code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *endptr, *str, newstr[20];
float val, newval;
if (argc < 2) {
fprintf(stderr, "Usage: %s number\n", argv[0]);
exit(1);
}
str = argv[1];
printf("\nstr = %s\nsizeof str = %d\nstrlen str = %d\n", str, sizeof(str),
strlen(str));
strcpy(newstr,argv[1]);
printf("\nnewstr = %s\nsizeof str = %d\nstrlen str = %d\n\n", newstr,
sizeof(newstr), strlen(newstr));
val = strtof(str, &endptr);
newval = strtof(newstr, &endptr);
if (*endptr != '\0')
printf("Additional characters after number: %s\n", endptr);
printf("str:\tstrtof of %s = %f\n", str, val);
printf("newstr:\tstrtof of %s = %f\n\n", newstr, newval);
return(0);
}
The code compiles without error or warning with: "gcc -o prg/tmp src/tmp.c"
Here is the bewildering output:
20:03 Rankin-P35a~/linux/programming/c> ./prg/tmp 250.871
str = 250.871
sizeof str = 4
strlen str = 7
newstr = 250.871
sizeof str = 20
strlen str = 7
str: strtof of 250.871 = 1132125952.000000
newstr: strtof of 250.871 = 1132125952.000000
Obviously I must be missing something very simple, but I can't put my hands on
it. Help?
--
David C. Rankin, J.D., P.E.
Rankin Law Firm, PLLC
510 Ochiltree Street
Nacogdoches, Texas 75961
Telephone: (936) 715-9333
Facsimile: (936) 715-9339
www.rankinlawfirm.com
I was comparing input handling between perl and c and I ran into a very weird
problem with the strtof function in c. What am I doing wrong? strtof doesn't
seem to be working like the man page shows. Here is my test bit of code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *endptr, *str, newstr[20];
float val, newval;
if (argc < 2) {
fprintf(stderr, "Usage: %s number\n", argv[0]);
exit(1);
}
str = argv[1];
printf("\nstr = %s\nsizeof str = %d\nstrlen str = %d\n", str, sizeof(str),
strlen(str));
strcpy(newstr,argv[1]);
printf("\nnewstr = %s\nsizeof str = %d\nstrlen str = %d\n\n", newstr,
sizeof(newstr), strlen(newstr));
val = strtof(str, &endptr);
newval = strtof(newstr, &endptr);
if (*endptr != '\0')
printf("Additional characters after number: %s\n", endptr);
printf("str:\tstrtof of %s = %f\n", str, val);
printf("newstr:\tstrtof of %s = %f\n\n", newstr, newval);
return(0);
}
The code compiles without error or warning with: "gcc -o prg/tmp src/tmp.c"
Here is the bewildering output:
20:03 Rankin-P35a~/linux/programming/c> ./prg/tmp 250.871
str = 250.871
sizeof str = 4
strlen str = 7
newstr = 250.871
sizeof str = 20
strlen str = 7
str: strtof of 250.871 = 1132125952.000000
newstr: strtof of 250.871 = 1132125952.000000
Obviously I must be missing something very simple, but I can't put my hands on
it. Help?
--
David C. Rankin, J.D., P.E.
Rankin Law Firm, PLLC
510 Ochiltree Street
Nacogdoches, Texas 75961
Telephone: (936) 715-9333
Facsimile: (936) 715-9339
www.rankinlawfirm.com
--
To unsubscribe, e-mail: opensuse+***@opensuse.org
For additional commands, e-mail: opensuse+***@opensuse.org
To unsubscribe, e-mail: opensuse+***@opensuse.org
For additional commands, e-mail: opensuse+***@opensuse.org