strtod - 将字符串转换为double的问题

时间:2011-03-28 11:24:17

标签: c char double strtod

我有一个非常小的程序,它将字符串转换为double。问题是每次打印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

2 个答案:

答案 0 :(得分:5)

尝试包含

#include <stdlib.h>

答案 1 :(得分:1)

你没有包含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