delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/06/23/17:27:17

From: benny AT crocodial DOT de (Benjamin Riefenstahl)
Subject: Re: scanf "%n" format specifier is not supported
23 Jun 1998 17:27:17 -0700 :
Message-ID: <358F7B56.8723BF20.cygnus.gnu-win32@crocodial.de>
References: <Pine DOT GSO DOT 3 DOT 96 DOT 980622162749 DOT 14664D-100000 AT sigma>
Mime-Version: 1.0
To: gnu-win32 AT cygnus DOT com

Hi Alexander,


Alexander Chernov wrote:
> ...For example, the following piece of code
> left variable n value as 0 instead of 2. ...
>
> #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;
> }

The problem is that the scanner gives up as it doesn't find the space
that your format string specifies. The %n is not processed, and so n is
left unchanged. Try this:

#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, r);

  r = sscanf("32", "%d%n", &v, &n);
  printf("v = %d\nn = %d\nr = %d\n", v, n, r);

  return 0;
}

Should output

v = 32
n = 0
r = 1
v = 32
n = 2
r = 1


so long, benny
======================================
Benjamin Riefenstahl (benny AT crocodial DOT de)
Crocodial Communications EntwicklungsGmbH
Ruhrstraße 61, D-22761 Hamburg, Germany
-
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