strtod 接受“e”但也有“d”。 - 为什么?
我觉得这很奇怪。
虽然 strtod 接受“e”作为输入字符串中的字符之一(准确地说是一个)是有道理的,但我发现它也接受“d”。
有人可以解释一下吗?
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你所说的“接受”是什么意思?这是我得到的输出
strtod (与 C 中的所有其他转换例程一样)解析字符串,直到找到“不属于”的字符。该字符不被视为错误,它只是被视为终止符。如果是
"1d1"
,则仅解析第一个"1"
,并且解析在'd'
处停止。转换的结果是1.0
(应该是这样)。如果您要求
strtod
从每次调用中返回“结束指针”,您会看到该指针指向带有的输入字符串的字符
(这同样适用于'd'
'd''a'
和'f'
)。如果您得到不同的结果,则一定是您正在使用的实现的一个怪癖。
What do you mean by "accepts"? This is the output I'm getting
strtod
(as all other conversion routines in C) parses the string until it finds the character that "doesn't belong". That character is not considered an error, it is simply treated as a terminator character. In case of"1d1"
only the first"1"
is parsed and the parsing stops at'd'
. The result of conversion is1.0
(as it should be).If you asked
strtod
to return the "end pointer" from each call, you'd see that the pointer points to character'd'
for the input string with'd'
in it (the same applies to'a'
and'f'
as well).If you are getting a different result, it must be a quirk of the implementation you are using.
您使用什么编译器/库来编译此代码?假设您使用的是 Visual Studio,这种行为是正常的(引用来自 MSDN 的文本):
您可以找到
strtod
的完整文档 此处该库的其他实现可能会支持类似的功能。但是,此处找到了
strtod
的手册页,但并没有请勿声明d
被识别为用于转换目的的有效字符。在这种情况下,它会导致输入字符串的解析停止,并且只有在该点之前解析的字符才会被转换(对于包含a
和f
)。也许您应该查看库实现的文档,并找出
strtod
能够针对您的具体情况进行解析的格式。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
strtod
hereOther implementations of the library would probably support something similar. However, the man-page for
strtod
found here, doesn't state thatd
is 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 containinga
andf
).Perhaps you should look at documentation for your library implementation and find out the format that
strtod
would be able to parse for your specific case.使用 D 而不是 E 是标记
double
数据(而不是float
)的 Fortran 方法。您可能正在使用接受它作为扩展的标准库。Use of D instead of E is the Fortran way to mark out
double
data (instead offloat
). You probably are using a standard library which accept it as an extension.哪个平台和编译器?在 Linux 和 Mac OS XI 中都会得到以下结果:
::: [1.000000] [10.000000] [1.000000] [1.000000]
这意味着只有
e
有效。请注意,您没有检查错误...Which platform and compiler? In both Linux and Mac OS X I get this result:
::: [1.000000] [10.000000] [1.000000] [1.000000]
which means only
e
worked. Note that you didn't check for errors...