Re: [PATCH] Re: sscanf("-1", "%d", &i) fails, returns 0

Ray Lee (ray-lk@madrabbit.org)
11 Nov 2002 12:25:34 -0800


On Mon, 2002-11-11 at 11:09, Randy.Dunlap wrote:
> | It only sounds strange at first. It actually means that scanf is
> | consistent with C's rules of assignment between mixed types. For
> | example:
<snip>
> These values (-100 and 4294967196) are the same bits, just observed/used
> in a different manner.

I probably didn't state it very clearly, but that was what I was getting
at. It's the same value after a cast. As Andreas points out though, it
may not be bit-for-bit identical on machines that don't use two's
complement. That's why the conversion code cares about the format
specifiers, it seems.

> Now what does it mean to "convert the value to an unsigned and return
> that." This is the same as above, isn't it?

Explicitly, in the scan conversion you'd do a:

unsigned int *u = (unsigned int *) va_arg(args,long long *);
*u = (unsigned int) converted_value;

...from wherever you get converted_value from (simple_strtoul? I haven't
looked at that routine). (The cast here is implied if left off, I'm just
being explicit.)

> I.e., on the scanf() side, there is no conversion needed; just store the
> value.

Almost true, as Andreas pointed out. You have to be careful that the
target you're storing to has the correctly declared type inside the
conversion routine.

Ray

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/