specifier "%n" when using "scanf"

Discussion in 'C++' started by thomas, Aug 18, 2009.

  1. thomas

    thomas Guest

    Hi,
    I used to see a format specifier "%s%n" for "scanf", like
    follows.

    ------------code---------
    #include<iostream>
    using namespace std;

    int main(){
    char xx[20];
    strcpy(xx, "ab ad ef");
    char yy[20]; int jj;
    cout<<sscanf(xx, "%s%n", yy, &jj)<<endl;
    cout<<yy<<" "<<jj<<endl;
    }
    --------code-----------

    But I didn't find any explaination about the "%n" usage in the web or
    text books.
    Can anyone give some references?
    thomas, Aug 18, 2009
    #1
    1. Advertising

  2. thomas wrote:
    > Hi,
    > I used to see a format specifier "%s%n" for "scanf", like
    > follows.
    >
    > ------------code---------
    > #include<iostream>
    > using namespace std;
    >
    > int main(){
    > char xx[20];
    > strcpy(xx, "ab ad ef");
    > char yy[20]; int jj;
    > cout<<sscanf(xx, "%s%n", yy, &jj)<<endl;
    > cout<<yy<<" "<<jj<<endl;
    > }
    > --------code-----------
    >
    > But I didn't find any explaination about the "%n" usage in the web or
    > text books.


    It returns the number of bytes consumed by sscanf by that point, provided it
    gets to that point.

    > Can anyone give some references?


    http://www.opengroup.org/onlinepubs/009695399/functions/scanf.html

    --
    Max
    Maxim Yegorushkin, Aug 18, 2009
    #2
    1. Advertising

  3. thomas wrote:
    > Hi,
    > I used to see a format specifier "%s%n" for "scanf", like
    > follows.


    C++1998 is based on C90, so technically scanf is part of C++.
    Nevertheless the function is described by the C standard and
    is rarely mentioned in literature about C++.

    > [...]
    > But I didn't find any explaination about the "%n" usage in the
    > web or text books.


    Hard to believe.

    > Can anyone give some references?


    http://man-wiki.net/index.php/3:fscanf
    # n Nothing is expected; instead, the number of characters consumed
    # thus far from the input is stored through the next pointer, which
    # must be a pointer to int. This is not a conversion, although it can
    # be suppressed with the * assignment-suppression character. The C
    # standard says: "Execution of a %n directive does not increment the
    # assignment count returned at the completion of execution" but the
    # Corrigendum seems to contradict this. Probably it is wise not to make
    # any assumptions on the effect of %n conversions on the return value.

    http://msdn.microsoft.com/en-us/library/6ttkkkhh(VS.71).aspx
    # n No input read from stream or buffer.
    # Pointer to int, into which is stored number of characters successfully
    # read from stream or buffer up to that point in current call to scanf
    # functions or wscanf functions.

    --
    Brüder, in die Tonne die Freiheit,
    Brüder, ein Stoppschild davor.
    Egal was die Schwarzen Verlangen
    Rufen wir: Ja! Brav im Chor.
    Alexander Bartolich, Aug 18, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Dutoit
    Replies:
    1
    Views:
    3,446
    Justin Dutoit
    Oct 5, 2004
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,338
    those who know me have no need of my name
    Apr 3, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    656
    Richard Bos
    May 2, 2006
  4. regis
    Replies:
    8
    Views:
    441
    P.J. Plauger
    Apr 28, 2006
  5. vlsidesign
    Replies:
    2
    Views:
    408
    Keith Thompson
    Nov 14, 2007
Loading...

Share This Page