printf %n question

J

john

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?
 
T

tragomaskhalos

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.
 
J

Jack Klein

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
 
C

Chip Coldwell

tragomaskhalos said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top