delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/06/24/10:07:23

From: rpj AT ise DOT canberra DOT edu DOT au (Ross Johnson)
Subject: Re: scanf "%n" format specifier is not supported
24 Jun 1998 10:07:23 -0700 :
Message-ID: <Pine.LNX.3.95.980624081446.772A-100000.cygnus.gnu-win32@swan.canberra.edu.au>
References: <358F0208 DOT CB0AC36F AT wgn DOT net>
Mime-Version: 1.0
To: $Bill Luebkert <dbe AT wgn DOT net>
Cc: "gnu-win32 AT cygnus DOT com" <gnu-win32 AT cygnus DOT com>

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: rpj AT ise DOT canberra DOT edu DOT au
| 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
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019