【发布时间】:2015-05-16 10:03:06
【问题描述】:
我希望endptr 指向与strtod() 和strtold() 相同的值。然而它们不同。我怀疑strtold() 不正确。 OTOH,这可能是规格不明确且任一结果都可接受的情况。
这是一个错误(以及使用哪个函数)还是未定义/未指定的行为?
使用:gcc\x86_64-pc-cygwin\4.8.3
#include <math.h>
#include <stdio.h>
// Edit - This include is in my true code, but was missing in original post
#include <stdlib.h>
int main(void) {
char *s = "123ez";
char *endptr;
double d = strtod(s, &endptr);
printf(" double %f '%s'\n", d, endptr);
long double ld = strtold(s, &endptr);
printf("long double %Lf '%s'\n", ld, endptr);
return 0;
}
Output:
double 123.000000 'ez'
long double 123.000000 'z'
[编辑]
gcc 命令gcc -I"C:\cygwin64\lib\gcc\x86_64-pc-cygwin\4.8.3\include" -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -MMD -MP -MF"string_fp.d" -MT"string_fp.d" -o "string_fp.o" "../string_fp.c"
GCC 4.9.2
【问题讨论】:
-
ideone.com/sCh4tn 为什么 ideone 不能给出正确的数字...
-
@HuStmpHrrr:缺少标题:ideone.com/IIBYu0 我们无法将
-Wall传递给 ideone,但如果这样做,我们会看到两个关于函数隐式声明的警告。 -
@BillLynch 我直接复制了 OP 的代码。这会是 op 没有正确输出的原因吗?
-
@HuStmpHrrr:chux 错过了一个标题。
strtod()在stdlib.h中定义。可能发生的情况是 cygwin 的 C 库从代码的标题中隐式包含stdlib.h。但是 glibc(ideone 使用)没有。因此,为了使代码不是未定义的行为,我们应该包含该标头。 -
@BillLynch 感谢您的想法。我的真实代码确实包括
#include <stdlib.h>,我确实看到我在这里的剪切/粘贴中错过了它。对不起,错误的线索。我会更新我的帖子。