strtod - 将字符串转换为双精度时出现问题

发布于 2024-10-27 17:06:41 字数 306 浏览 4 评论 0原文

我有一个非常小的程序,它将字符串转换为双精度。问题是每次打印 0.0000 时。 请帮我。

提前致谢。

enter code here

$ export LT_LEAK_START=1.5
$ echo $LT_LEAK_START
  1.5

#include <stdio.h>

int main()
{
  double d;
  d=strtod(getenv("LT_LEAK_START"), NULL);
  printf("d = %lf\n",d);
}
Output:
d=0.0000000

I have a very small program which converts a string to double. The problem is everytime it is printing 0.0000.
Please help me.

Thanks in advance.

enter code here

$ export LT_LEAK_START=1.5
$ echo $LT_LEAK_START
  1.5

#include <stdio.h>

int main()
{
  double d;
  d=strtod(getenv("LT_LEAK_START"), NULL);
  printf("d = %lf\n",d);
}
Output:
d=0.0000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-11-03 17:06:41

尝试包括

#include <stdlib.h>

Try including

#include <stdlib.h>
櫻之舞 2024-11-03 17:06:41

您不包括 strtod decl 标头(stdlib.h),因此您使用内部 strtod 实现(这似乎只是一个存根?)

root@pinkpony:~# gcc -Wall -g  -o t t.c
t.c: In function ‘main’:
t.c:6: warning: implicit declaration of function ‘strtod’
t.c:6: warning: implicit declaration of function ‘getenv’
t.c:8: warning: control reaches end of non-void function
root@pinkpony:~# gdb ./t
Reading symbols from /root/t...done.
(gdb) run
Starting program: /root/t 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
5    30     strtod_l.c: No such file or directory.
        in strtod_l.c
(gdb) bt
#0  0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
#1  0x00000000004005bc in main () at t.c:6
(gdb) 

忽略 sigsegv,在我的平台上是由 getenv() 引起的,它也被声明在 stdlib 中,但没有内部 gcc impl

you aren't including the strtod decl header (stdlib.h) so you are using an internal strtod implementation (which seems to be just a stub?)

root@pinkpony:~# gcc -Wall -g  -o t t.c
t.c: In function ‘main’:
t.c:6: warning: implicit declaration of function ‘strtod’
t.c:6: warning: implicit declaration of function ‘getenv’
t.c:8: warning: control reaches end of non-void function
root@pinkpony:~# gdb ./t
Reading symbols from /root/t...done.
(gdb) run
Starting program: /root/t 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
5    30     strtod_l.c: No such file or directory.
        in strtod_l.c
(gdb) bt
#0  0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
#1  0x00000000004005bc in main () at t.c:6
(gdb) 

ignore the sigsegv, on my platform is caused by getenv() which is declared as well in stdlib but has no internal gcc impl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文