Subject: RE: scanf "%n" format specifier is not supported
From: [email-masked] (Alexander Chernov)

23 Jun 1998 17:57:15 -0700
Message-ID: <Pine.GSO.3.96.980623154135.26282A-100000.cygnus.gnu-win32@sigma>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
To: GNU-Win32 <[email-masked]>

On Tue, 23 Jun 1998, Colin Peters wrote:

> Date: Tue, 23 Jun 1998 13:03:31 +0900
> From: Colin Peters <[email-masked]>
> To: Alexander Chernov <[email-masked]>
> Cc: GNU-Win32 <[email-masked]>
> Subject: RE: scanf "%n" format specifier is not supported
> 
> Alexander Chernov <[email-masked]> wrote:
> >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);
> 
> should be: printf("v = %d\nn = %d\nr = %d\n", v, n, r);

Yes.

> >  return 0;
> >}
> 
> 
> Aside from the typo above the reason the code doesn't work is that sscanf
> never gets as far as the %n. It stops when it reaches the end of the string
> "32" while looking for a whitespace to match the space between %d and %n.
> Try adding a space to the end of "32" like this:
> 
> r = sscanf("32 ", "%d %n", &v, &n);

Ok, but atleast on Linux, HPUX and Solaris sscanf works as described
above,
ie "r = sscanf("32", "%d %n", &v, &n);" sets r to 1, v to 32 and n to 2.
I looked at my C book, and It says nothing on this topic, though...

> 
> And, with Mingw32 at least, you'll get:
> v = 32
> n = 3
> r = 1
> 
> Alternatively you could change the format string to "%d%n" and n will be set
> to 2.

I've replaced all sscanf with strtol, which provides better diagnostics.

> 
> Hope this helps,
> Colin.
> 
> -- Colin Peters - colin at fu.is.saga-u.ac.jp
> -- Saga Univ. Dept. of Information Science
> -- http://www.geocities.com/Tokyo/Towers/6162/index.html
> -- http://www.fu.is.saga-u.ac.jp/~colin/index.html
> 
> 
> 

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