Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C parsing double from argument strings

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?

like image 251
LandonSchropp Avatar asked Feb 08 '10 08:02

LandonSchropp


People also ask

How to read a double from a string in C?

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.

What is double parse?

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.


1 Answers

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!

like image 86
John Carter Avatar answered Sep 17 '22 15:09

John Carter