Calling strtof with a floating point number runs fine on my local machine but on the school’s servers strtof always returns 0.000000. I checked to see if there was anything stored in errno since a 0 should mean an error, but it says success. Does anyone have an idea why this might be?
Here is the code.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%f\n", strtof(argv[1],0));
return 0;
}
Short version: compile with
-std=gnu99or-std=c99. Explanation follows.I’ve reproduced a similar “problem” on my own box. However, when I try to compile:
So I looked at the
manpage forstrtof(), and it says:What that means is that one of those values has to be
#defined before includingstdlib.h. However, I just recompiled with-std=gnu99, and that defines one of those for me and it works.Moral: always compile with
-Wall. 😉