My C is rusty. I am trying to convert a string to float (or double). I have tried atof() and strtof(), and tried following instructions here and here. No luck.
Here's my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
float x;
char a[20];
strcpy(a,argv[1]);
x = strtof(a);
printf("%s\n",a);
printf("%f\n",x);
return 0;
}
No luck. Here's output, on the command line:
prompt$ ./a.out 2.34
2.34
1426063.000000
prompt$
If I use atoi() instead, I get 0.0.
Clearly there is something simple I'm missing. This is with gcc 4.8.4 on ubuntu fwiw.
Makes no difference if I compile with -static flag, or use double instead of float (and %e instead of %f), or include math.h. The output changes in some of those cases, but it's still nonsense.
Thanks, Peter