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

Ray Lee (ray-lk@madrabbit.org)
11 Nov 2002 06:54:54 -0800


> > 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/