behavior of printf

P

pai

Hi ,

Can any one tell me how this statement of printf is behaving . how
the last digit is printed

int a=2,b=4,c=7;

printf("%d",printf("%d %d:",a,b));
//answer to this was 2 4:3

printf("%d",printf("%d %d %d:",a,b,c));
//answer to this 2 4 7:6

Can any one explain this behavior ?

Thanks
Pai........
 
E

Eric Sosman

pai wrote On 05/01/06 14:12,:
Hi ,

Can any one tell me how this statement of printf is behaving . how
the last digit is printed

int a=2,b=4,c=7;

printf("%d",printf("%d %d:",a,b));
//answer to this was 2 4:3

The output ought to have been "2 4:4" -- either you've
mis-reported it, or your C implementation is broken.
printf("%d",printf("%d %d %d:",a,b,c));
//answer to this 2 4 7:6

This looks right.
Can any one explain this behavior ?

Yes, I can! And so can you, if you study it carefully.
Pretend you're the computer, mechanically executing the
first of the two puzzling statements:

First, you're confronted with a printf() call whose
basic form is `printf(format, argument);'. Before you
can actually execute the call, you need to evaluate the
format (easy: it's a string literal) and the second
argument. That second argument is ...

... a function call, namely, a call to printf(). So
you (in your persona as the computer) execute this printf()
call, which produces the output "2 4:". Also, printf()
returns a value: the number of characters that were output.
The call generated four characters, so the value is 4.

Back to the outer printf(), which now looks like
`printf("%d", 4);'. This call produces the output "4".
It also returns the number of characters of output (1),
but since you don't use that value it's simply ignored.

All together, the inner printf() outputs "2 4:" and
the outer printf() tacks on another "4", so the complete
output should be "2 4:4". Accept no substitutes.

Exercise for the reader: Analyze the second statement
the same way.
 
P

pai

Thanks eric for that explation . Actually I didnt know that printf
returned number of characters which it prints . In manpage of printf
there was nothing about return type of printf .

Thanks
Pai..
 
C

CBFalconer

pai said:
Thanks eric for that explation . Actually I didnt know that printf
returned number of characters which it prints . In manpage of printf
there was nothing about return type of printf .

Don't top-post. Your answer belongs after, or intermixed with, the
snipped quoted material to which you reply.

That simply indicates that your man pages are sadly lacking. For
the definitive descriptions of standard C routines, see the C
standards. Search for N1124 and/or N869.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

Coos Haak

Op 2 May 2006 02:57:09 -0700 schreef pai:
Thanks eric for that explation . Actually I didnt know that printf
returned number of characters which it prints . In manpage of printf
there was nothing about return type of printf .

Thanks
Pai..
<OT>
There is more than one man page about printf.
Try: man 3 printf
</OT>
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top