I'm trying to parse an argument value in C and convert the number to a double value. I have:
char *stringEnd;
double num = strtod("123.0", &stringEnd);
I used "123.0"
just to test the function, but it always returns a value of 0.0
. Does anybody know what I'm doing wrong?
In the C Programming Language, the strtod function converts a string to a double. The strtod function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that isn't a number.
Converts the string representation of a number to its double-precision floating-point number equivalent. Parse(String, NumberStyles) Converts the string representation of a number in a specified style to its double-precision floating-point number equivalent.
Are you including the relevant header? ie: #include <stdlib.h>
First though (and you should be doing this all the time anyway), try compiling with all warnings on (-Wall
on GCC).
If you get a warning about strtod
being undefined, that shows where the problem is coming from.
This is a nasty one, because C will implicitly declare any function it doesn't have a prototype for as returning int
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With