>me2 wrote:
>> ... I use sscanf %i to convert argv[1] to an int. Then
>> I cast the int to an unsigned int and do the rest of the manipulation.
>> However, as the above example shows, it doesn't work properly when I input
>> a sign extended octal or hex number.
>>
>> The decimal of 0xffffff81 should be 4294967169, but it isn't. Why ?
In article < .com>
Scorpio <> wrote:
>Because %i is ...
Right start, but:
>only for integers.
wrong details.
All of this family of conversions -- %d, %i, %o, %u, and %x -- are
"for integers", specifically for int unless modified (e.g., %lu is
for long, %hx is for short; note that the hh and ll modifiers are
specific to C99). All of them also convert "as if" by strtol() or
strtoul() -- or in C99 sometimes strtoll() and strtoull() -- with
a base of 16, 10, 8, or 0 depending on the conversion directive;
except that in all cases, the behavior is not defined if the number
would overflow.
Most implementations seem to use the strtol() family of functions
internally, so that they all exhibit that family's "clamping"
behavior of out-of-range inputs. This is the case above: 0xffffff81
is (presuambly) out of range for strtol(), and %i reads a *signed*
integer, so the input is clamped to INT_MAX (or more likely to
LONG_MAX, but that is probably the same as INT_MAX on the target
platform).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.