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?

5
  • 1
    Welcome to the land of Unix. If you write to (one of) the POSIX standard(s) you will get more portability. But that can mean ignoring some very useful extensions. Dec 11, 2010 at 21:25
  • @dmckee: This has nothing to do with UNIX. It has to do with OP invoking UB by calling a function with the wrong signature. The desired behavior is not even POSIX-specific; it's required by ANSI/ISO C. Dec 11, 2010 at 22:02
  • @R.. Didn't even look at that. Just responded to the claim of system dependencies. Dec 11, 2010 at 22:09
  • @R..: There's absolutely nothing wrong with the call as specified in the question. What do you mean by "the wrong signature".
    – JeremyP
    Dec 11, 2010 at 22:16
  • @R.. How is the signature wrong? Dec 11, 2010 at 22:40

2 Answers 2

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 [...]

4
  • The compiler is GCC, version 3.2.2, provided by LynuxWorks. Dec 11, 2010 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, 2010 at 22:54
  • I guess I need to look up if 3.2.2 was C99 compliant, or just C89. Dec 11, 2010 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, 2010 at 9: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.

3
  • 2
    I think you mean strtol and friends
    – pmg
    Dec 11, 2010 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, 2010 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. Dec 11, 2010 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.