printf %n question

Discussion in 'C++' started by john, Oct 31, 2007.

  1. john

    john Guest

    The code:

    #include <cstdio>


    int main()
    {
    using namespace std;

    int i;

    printf("%ld\n%n", 1, &i);

    printf("==>%d\n", i);

    printf("%ld\n%n", 123456, &i);

    printf("==>%d\n", i);

    }

    in my system produces:

    [john@localhost src]$ ./foobar-cpp
    1
    ==>2
    123456
    ==>7

    [john@localhost src]$


    I expected:

    "[john@localhost src]$ ./foobar-cpp
    1
    ==>1
    123456
    ==>6
    [john@localhost src]$ "



    It looks like it increments the value written to i with 1 more, than it
    should. Is this a bug of my compiler, or am I wrong?
     
    john, Oct 31, 2007
    #1
    1. Advertising

  2. On 31 Oct, 09:42, john <> wrote:
    > The code:
    >
    > #include <cstdio>
    >
    > int main()
    > {
    > using namespace std;
    > int i;
    > printf("%ld\n%n", 1, &i);
    > printf("==>%d\n", i);
    > printf("%ld\n%n", 123456, &i);
    > printf("==>%d\n", i);
    >
    > }
    >
    > in my system produces:
    >
    > [john@localhost src]$ ./foobar-cpp
    > 1
    > ==>2
    > 123456
    > ==>7
    >
    > [john@localhost src]$
    >
    > I expected:
    >
    > "[john@localhost src]$ ./foobar-cpp
    > 1
    > ==>1
    > 123456
    > ==>6
    > [john@localhost src]$ "
    >
    > It looks like it increments the value written to i with 1 more, than it
    > should. Is this a bug of my compiler, or am I wrong?


    Interesting - have never come across %n before:
    Googling unearths (in http://www.cplusplus.com) :
    "Nothing printed. The argument must be a pointer to a signed int,
    where the number of characters written so far is stored."

    So your sample is behaving correctly:
    "1" + "\n" = 2 characters
    "123456" + "\n" = 7 characters
    as reported.
     
    tragomaskhalos, Oct 31, 2007
    #2
    1. Advertising

  3. john

    Jack Klein Guest

    On Wed, 31 Oct 2007 11:42:36 +0200, john <> wrote in
    comp.lang.c++:

    > The code:
    >
    > #include <cstdio>
    >
    >
    > int main()
    > {
    > using namespace std;
    >
    > int i;
    >
    > printf("%ld\n%n", 1, &i);
    >
    > printf("==>%d\n", i);
    >
    > printf("%ld\n%n", 123456, &i);
    >
    > printf("==>%d\n", i);
    >
    > }
    >
    > in my system produces:
    >
    > [john@localhost src]$ ./foobar-cpp
    > 1
    > ==>2
    > 123456
    > ==>7
    >
    > [john@localhost src]$
    >
    >
    > I expected:
    >
    > "[john@localhost src]$ ./foobar-cpp
    > 1
    > ==>1
    > 123456
    > ==>6
    > [john@localhost src]$ "
    >
    >
    >
    > It looks like it increments the value written to i with 1 more, than it
    > should. Is this a bug of my compiler, or am I wrong?


    You are wrong. You put the %n after the newline character in the
    format string, so it is counted as well. Try putting the %n before
    the \n.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 1, 2007
    #3
  4. tragomaskhalos <> writes:

    > On 31 Oct, 09:42, john <> wrote:
    >> The code:
    >>
    >> #include <cstdio>
    >>
    >> int main()
    >> {
    >> using namespace std;
    >> int i;
    >> printf("%ld\n%n", 1, &i);
    >> printf("==>%d\n", i);
    >> printf("%ld\n%n", 123456, &i);
    >> printf("==>%d\n", i);
    >>
    >> }
    >>
    >> in my system produces:
    >>
    >> [john@localhost src]$ ./foobar-cpp
    >> 1
    >> ==>2
    >> 123456
    >> ==>7
    >>
    >> [john@localhost src]$
    >>
    >> I expected:
    >>
    >> "[john@localhost src]$ ./foobar-cpp
    >> 1
    >> ==>1
    >> 123456
    >> ==>6
    >> [john@localhost src]$ "
    >>
    >> It looks like it increments the value written to i with 1 more, than it
    >> should. Is this a bug of my compiler, or am I wrong?

    >
    > Interesting - have never come across %n before:


    Very useful for IOCCC entries.

    Chip

    --
    Charles M. "Chip" Coldwell
    "Turn on, log in, tune out"
    Somerville, Massachusetts, New England
     
    Chip Coldwell, Nov 1, 2007
    #4
    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. Edith Gross
    Replies:
    2
    Views:
    353
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    663
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,331
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    456
  5. guru
    Replies:
    8
    Views:
    302
Loading...

Share This Page