Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > printf %n question

Reply
Thread Tools

printf %n question

 
 
john
Guest
Posts: n/a
 
      10-31-2007
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?
 
Reply With Quote
 
 
 
 
tragomaskhalos
Guest
Posts: n/a
 
      10-31-2007
On 31 Oct, 09:42, john <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      11-01-2007
On Wed, 31 Oct 2007 11:42:36 +0200, john <(E-Mail Removed)> 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
 
Reply With Quote
 
Chip Coldwell
Guest
Posts: n/a
 
      11-01-2007
tragomaskhalos <(E-Mail Removed)> writes:

> On 31 Oct, 09:42, john <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Question about evaluating the arguments of printf (maybe OT?) Edith Gross C++ 2 11-02-2003 12:16 PM



Advertisments