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

From: Ray Lee (ray-lk@madrabbit.org)
Date: Mon Nov 11 2002 - 09:54:54 EST


> > What should it do?
> I would model this after user space. (Which does strange things:

<snip>

It only sounds strange at first. It actually means that scanf is
consistent with C's rules of assignment between mixed types. For
example:

ray:~$ cat signs.c

#include <stdio.h>

main() {
        char scan[]="-100";
        unsigned int u;
        int i;

        sscanf(scan, "%ud", &u);
        sscanf(scan, "%d", &i);
        printf("%s scanned to signed %d and unsigned %u\n", scan, i, u);

        i=-100;
        u=i;
        printf("%d assigned to unsigned int gives %u\n", i, u);
}

ray:~$ ./signs
-100 scanned to signed -100 and unsigned 4294967196
-100 assigned to unsigned int gives 4294967196

So, one should think of scanf as having correct knowledge of the types
it's scanning, and then shoe-horning the result into whatever you asked
for. Just like C itself.

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/



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