2

It seems that LynxOS's implementation of strtod doesn't handle all the same cases as that of Linux, or for that matter Solaris. The problem I have is that I am trying to parse some text that can have decimal or hexadecimal numbers in it.

On Linux I call

a = strtod(pStr, (char **)NULL);

and I get the expected values in a for input strings such as 1.234567 and 0x40.

On LynxOS, the decimal numbers parse correctly, but the hex parses simply as 0 due to stopping when it hits the 'x'. Looking at the man pages, it seems that LynxOS's strtod only supports decimal strings in the input.

Does anyone here know of an alternative that will work on both Lynx and Linux?

LordOphidian
  • 178
  • 9

2 Answers2

2

Quote from the Standard (7.20.1.3) ( http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf )

The expected form of the subject sequence is an optional plus or minus sign, then one of the following:
— a nonempty sequence of decimal digits optionally containing a decimal-point character, then an optional exponent part as defined in 6.4.4.2;
— a 0x or 0X, then a nonempty sequence of hexadecimal digits optionally containing a decimal-point character, then an optional binary exponent part as defined in 6.4.4.2;
— [...]

So, the compiler you're using on LynxOS is not a C99 compiler.


My copy of the C89 Standard has no reference to the 0x prefix:

4.10.1.4 The strtod function

[...]

The expected form of the subject sequence is an optional plus or minus sign, then a nonempty sequence of digits optionally containing a decimal-point character, then an optional exponent part [...]

pmg
  • 106,608
  • 13
  • 126
  • 198
  • The compiler is GCC, version 3.2.2, provided by LynuxWorks. – LordOphidian Dec 11 '10 at 22:37
  • Hmmm ... my answer edited to reflect `C99`. As far as I can tell (my version of the C89 Standard is not trustworthy), for `C89` there was no requirement for `0x`. – pmg Dec 11 '10 at 22:54
  • I guess I need to look up if 3.2.2 was `C99` compliant, or just `C89`. – LordOphidian Dec 11 '10 at 22:58
  • 2
    @LordOphidian: In this case it is not GCC that is of interest, but the C library you're using (`strtod()` is not implemented by GCC). – caf Dec 12 '10 at 09:42
-2

strtod takes 3 arguments, not two. If you had prototyped it by including the correct header (stdlib.h), your compiler would have issued an error. Since you're calling a function with the wrong signature, your program has undefined behavior. Fix this and everything will be fine.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    I think you mean `strtol` and friends – pmg Dec 11 '10 at 22:13
  • 1
    -1 Incorrect, `strtod` has the following prototype: `double strtod(const char * restrict nptr, char ** restrict endptr);` - taken directly from the C99 standard. – JeremyP Dec 11 '10 at 22:21
  • Indeed, I misread and thought the question was about `strtol`. For what it's worth, C99 requires `strtod` to support hex floats (which require an exponent, if I remember correctly) but C89 doesn't since they didn't exist in C89. – R.. GitHub STOP HELPING ICE Dec 11 '10 at 23:31