[comp.lang.c] What should [fs]scanf return?

dal@midgard.Midgard.MN.ORG (Dale Schumacher) (01/20/89)

The following question appeared in comp.os.minix recently.  I thought
it would get a better/faster answer in comp.lang.c, thus this crossposting.

In article <588@forty2.UUCP> poole@forty2.UUCP (Simon Poole) writes:
|Somehow I've got the feeling that this has been hashed thru before, but
|I probably wasn't paying attention then. Consider the following code
|fragment:
|
|........
|    printf("%d\n",sscanf("123","%d",&dummy));
|    printf("%d\n",sscanf("123","%*d",&dummy));   
|    printf("%d\n",sscanf("123:456","%d:%d",&dummy,&dummy));
|    printf("%d\n",sscanf("123:456","%*d:%*d"));
|........
|
|What should it print?
|
|Minix-ST 1.1     BSD 4.2    I think it should be   
|
|    1             1                1
|    1             -1               0
|    3             2                2
|    3             -1               0
|
|
|K&R first edition says (about scanf):
|
|'It returns as its value the number of sucessfully matched and assigned
| input items'

guy@auspex.UUCP (Guy Harris) (01/21/89)

>...I think it should be...

You're right.  It should be.  (And it is, on SunOS 4.0, which means it
probably is on S5R3 as well, since the 4.0 "scanf" code is derived from
the S5R3 "scanf" code....)

I don't know why BSD is confused; perhaps it treats suppressed
assignments as errors, or assumes that just because there were no
succesful assignments (because there were no assignments at all) the
call must have failed.  That's wrong.  I assume Minix is confused
because it 1) counts even suppressed assignments and 2) counts literal
matches (i.e., the ":" matched the ":"); that's also wrong.