I find this strange.
While it makes sense that strtod accepts ‘e’ as one of the characters (exactly one to be precise) in the input string I find that it also accepts ‘d’.
Can someone please 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;
}
What Compiler/Libraries are you using to compile this code? Assuming you’re on Visual Studio, this behaviour is expected (quoting text from the MSDN):
You can find the full documentation for
strtodhereOther implementations of the library would probably support something similar. However, the man-page for
strtodfound here, doesn’t state thatdis recognized as a valid character for the conversion purpose. In such a case, it would cause the parsing of the input string to stop and only characters parsed until that point would be converted (the same happens for the strings containingaandf).Perhaps you should look at documentation for your library implementation and find out the format that
strtodwould be able to parse for your specific case.