Strtod accepts "e", but also "d" - why?

I find it strange.

Although it makes sense that strtod accepts "e" as one of the characters (exactly one to be exact) in the input string, I find that it also accepts "d".

Can someone explain?

#include < stdio.h >
#include < stdlib.h >
int main ()
{
char *s[] = {"1a1", "1e1", "1d1", "1f1"};
char * pEnd;
double d0, d1, d2, d3;
d0 = strtod (s[0],&pEnd);
d1 = strtod (s[1],NULL);
d2 = strtod (s[2],NULL);
d3 = strtod (s[3],NULL);
printf ("::: [%f] [%f] [%f] [%f] \n", d0, d1, d2, d3);
return 0;
}
+3
source share
4 answers

What compilers / libraries do you use to compile this code? Assuming you are working in Visual Studio, this behavior is expected (quoting text from MSDN):

strtod expects nptr to point to a line like this:

[spaces] [character] [numbers] [.digits] [{d | D | e | E} [character] digits]

You can find the full documentation for strtod here.

, , - . man- strtod , , d , , , ( , a f).

, , strtod .

+3

""? ,

::: [1.000000] [10.000000] [1.000000] [1.000000]

strtod ( C) , , " ". , . "1d1" "1", 'd'. 1.0 ( ).

strtod " " , , 'd' 'd' ( 'a' 'f').

, , .

+4

Using D instead of E is Fortran's way of highlighting data double(instead float). You are probably using a standard library that accepts it as an extension.

+1
source

What platform and compiler? On Linux and Mac OS X, I get this result:

::: [1.000000] [10.000000] [1.000000] [1.000000]

which means only e. Please note that you did not check for errors ...

0
source

All Articles