开发者

LynxOS strtod not the same as Linux

开发者 https://www.devze.com 2023-01-30 16:30 出处:网络
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 hav

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 hex开发者_运维问答adecimal 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消