12 points · by RiderOfGiraffes · 16y ago · 16 comments
Prior to C99 there was no strtof. Some people may have rolled their own, but it wasn't provided, you used strtod and cast to float if that's what you wanted.
Then C99 included strtof, so the code (and hence symbol) became available in the libraries. However, to avoid clashing with legacy code in which someone used their own version, the prototype is only declared if the compiler is in C99 mode.
That means that if you're not in C99 mode, you use the symbol strtof, and you don't declare the function for yourself, the compiler successfully finds the symbol in the library and links to the C99 code, but hasn't declared the prototype, so you don't get what you think you get.
And by default you don't get warned.
To repeat the moral: use -Wall.
"The moral? Always use -Wall"
This frequently results in me finding some subtle bug which otherwise could have taken hours of head scratching or single-stepping in gdb to locate.
C is a somewhat weak foundation for static program analysis but that doesn't mean we can't try to make the most out of it.
Arguably, allowing procedures to be called without prototypes is a bug in the C spec, but I doubt it'll be removed any time soon simply due to sheer volume of extant legacy code.
NAME
strtod, strtof, strtold - convert ASCII string to floating-point number
SYNOPSIS
#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
Furthermore, here's stdlib.h: extern float strtof (__const char *__restrict __nptr,
char **__restrict __endptr) __THROW __nonnull ((1)) __wur;
Perhaps it works on my machine because I'm not using an out-of-date version of GCC (gcc (Debian 4.4.4-2) 4.4.4)?"Apparently GCC has a built-in version of strtof, which it compiles into your code if you're not using c99."
No, your libc has a function called strtof which the compiled program links to. Just like any and every other external reference and completely independent of where the prototype comes from.
Summary in a comment elsewhere in this submission.