First my context is that of a compiler writer who needs to convert floating point literals (strings) into float/double values. I haven\'t done any floating point programming
@Nicholas Goy:
I don't think sscanf(str, "%f, &float) == 1 (or != 1) really does what's desired.
If there are additional characters in str (e.g. "1.1foo"), it will parse without error, which is probably undesirable. This can be rectified by doing:
char dummy;
if (sscanf(str, "%f%c", &float, &dummy) != 1) {
/* Parse error. */
} else {
/* Parsed cleanly. */
}
instead. However, sscanf with %f is likely to use strtod internally and cast to float anyway. The language in the C standard says:
a,e,f,g Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.
which sort of implies this, and it seems to be true for me (gcc 4.2.1 on FreeBSD). Using the above sscanf code, "1.79e308" parses successfully but has the value +inf, as does "5e-300" with the value 0.0, and these are the same results you'd get from (float) 1.79e308 and (float) 5e-300.
Anyway. All that said, I question why the OP wants to use float instead of double anyway.