1

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
RajSanpui
  • 11,556
  • 32
  • 79
  • 146

2 Answers2

7

Try including

#include <stdlib.h>
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • @codaddict: If stdlib.h is required, by `strtod`, then why it doesn't give error on compiling? How to use it in C++? #include will work? – RajSanpui Mar 28 '11 at 11:29
  • @kingsmasher1: You're not compiling with warnings. For gcc, use `-Wall`. For c++ `#include ` – Erik Mar 28 '11 at 11:29
  • @Erik: I guess, without `-Wall` also gcc gives warnings for incompatible type conversions etc. Doesn't it? Then why `-Wall` for this case? – RajSanpui Mar 28 '11 at 11:32
  • @kingsmasher1: It's not a language error to use an undeclared function. It just has funny effects like assuming a function returns int if it's not declared. You should always compile with `-Wall`, it catches a lot of common mistakes. – Erik Mar 28 '11 at 11:36
  • @Codaddict: Thanks codaddict. – RajSanpui Mar 28 '11 at 11:40
1

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

user237419
  • 8,829
  • 4
  • 31
  • 38