473,509 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Oct 31 '07 #1
3 1845
On 31 Oct, 09:42, john <j...@no.spamwrote:
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.

Oct 31 '07 #2
On Wed, 31 Oct 2007 11:42:36 +0200, john <jo**@no.spamwrote 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
Nov 1 '07 #3
tragomaskhalos <da*************@logicacmg.comwrites:
On 31 Oct, 09:42, john <j...@no.spamwrote:
>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
Nov 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3045
by: Wenjie | last post by:
Hello, Is printf("%d", var); OK for the unsigned long var = SOME_VAR? Or should some other print format be specified? Thanks and best regards, Wenjie
14
5539
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
188
17200
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
31
2590
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
12
7577
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
11
3458
by: rahul8143 | last post by:
Does printf output depends on compilers or operating systems? I have one program int main() { int i=5; printf("%d %d %d",i++,i++,i++); return 0; } Output of this program on DOS (TC) 7 6 5...
27
2941
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
7
2820
by: Martin Jørgensen | last post by:
Hi, Perhaps a stupid question but I wonder if this isn't possible using C: printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": "); It would be nicer to have it on one...
5
1701
by: Why Tea | last post by:
int printf(const char *fmt,...) The fmt is const, indicating that it can't be changed during runtime. If I want to print a series of integers that I only know the largest value beforehand,...
7
5166
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.