Connecting Tech Pros Worldwide Help | Site Map

printf %n question

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 31st, 2007, 09:45 AM
john
Guest
 
Posts: n/a
Default printf %n question

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?

  #2  
Old October 31st, 2007, 10:05 AM
tragomaskhalos
Guest
 
Posts: n/a
Default Re: printf %n question

On 31 Oct, 09:42, john <j...@no.spamwrote:
Quote:
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.



  #3  
Old November 1st, 2007, 02:05 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: printf %n question

On Wed, 31 Oct 2007 11:42:36 +0200, john <john@no.spamwrote in
comp.lang.c++:
Quote:
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
  #4  
Old November 1st, 2007, 02:55 PM
Chip Coldwell
Guest
 
Posts: n/a
Default Re: printf %n question

tragomaskhalos <dave.du.vergier@logicacmg.comwrites:
Quote:
On 31 Oct, 09:42, john <j...@no.spamwrote:
Quote:
>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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.