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]