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

From: Ray Lee (
Date: Mon Nov 11 2002 - 09:54:54 EST

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


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

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);

        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.


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:22 EST