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

From: Ray Lee (
Date: Mon Nov 11 2002 - 15:25:34 EST

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:
> 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.


To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

This archive was generated by hypermail 2b29 : Fri Nov 15 2002 - 22:00:23 EST