Subject: Re: scanf "%n" format specifier is not supported
From: [email-masked] (Ross Johnson)

24 Jun 1998 10:07:23 -0700
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
To: $Bill Luebkert <[email-masked]>
Cc: "[email-masked]" <[email-masked]>

On Mon, 22 Jun 1998, $Bill Luebkert wrote:

> Alexander Chernov wrote:
> > 
> > Hello,
> > 
> > This looks like a missing feature: "%n" format specifier in sscanf
> > is not supported. For example, the following piece of code
> > left variable n value as 0 instead of 2. According to my textbooks
> > ANSI C specifies %n specifier.
> > 
> > #include <stdio.h>
> > #include <string.h>
> > 
> >   int
> > main()
> > {
> >   int v = 0, n = 0, r = 0;
> > 
> >   r = sscanf("32", "%d %n", &v, &n);
> >   printf("v = %d\nn = %d\nr = %d\n", v, n, d);
> >   return 0;
> > }
> 
> I would assume this is normal behavior since the %d sucked up the entire 
> "32" and left nothing for the %n.
> 

I found this problem when porting my own code to NT using VC++. After
finding that the POSIX standard doesn't explicitly state what %n should
do, the question was posted to the C language newsgroup where the
concensus response was that the Windows scanf function behavior is
incorrect. My own feeling is that it makes no sense to have a feature (%n) 
which is dependent on the input string.

It works as I and Alexander expected (ie. will return 2 in the example
above) on both Linux, SunOS and Solaris systems, and I think this is
correct. 

My workaround was to append a space character to the string before
scanning it. The example above may need two since it's format string looks
for a space character before the %n is seen.

+----------------------+---+
| Ross Johnson         |   | E-Mail: [email-masked]
| Info Sciences and Eng|___|
| University of Canberra   | FAX:    +61 6 2015227
| PO Box 1                 |
| Belconnen  ACT    2616   | WWW:    http://willow.canberra.edu.au/~rpj/
| AUSTRALIA                |
+--------------------------+

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".