Connecting Tech Pros Worldwide Forums | Help | Site Map

behavior of printf

pai
Guest
 
Posts: n/a
#1: May 1 '06
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........


Eric Sosman
Guest
 
Posts: n/a
#2: May 1 '06

re: behavior of printf




pai wrote On 05/01/06 14:12,:[color=blue]
> 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[/color]

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

This looks right.
[color=blue]
> Can any one explain this behavior ?[/color]

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.

--
Eric.Sosman@sun.com


--
Eric.Sosman@sun.com

pai
Guest
 
Posts: n/a
#3: May 2 '06

re: behavior of printf



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


Eric Sosman wrote:[color=blue]
> pai wrote On 05/01/06 14:12,:[color=green]
> > 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[/color]
>
> The output ought to have been "2 4:4" -- either you've
> mis-reported it, or your C implementation is broken.
>[color=green]
> > printf("%d",printf("%d %d %d:",a,b,c));
> > //answer to this 2 4 7:6[/color]
>
> This looks right.
>[color=green]
> > Can any one explain this behavior ?[/color]
>
> 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.
>
> --
> Eric.Sosman@sun.com
>
>
> --
> Eric.Sosman@sun.com[/color]

CBFalconer
Guest
 
Posts: n/a
#4: May 2 '06

re: behavior of printf


pai wrote:[color=blue]
>
> 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 .[/color]

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/>

Coos Haak
Guest
 
Posts: n/a
#5: May 2 '06

re: behavior of printf


Op 2 May 2006 02:57:09 -0700 schreef pai:
[color=blue]
> 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..
>[/color]
<OT>
There is more than one man page about printf.
Try: man 3 printf
</OT>
--
Coos
Closed Thread


Similar C / C++ bytes