Monday, February 14, 2011

what printf returns???????

Coming straightly to the point :the prototype of printf is:
int printf( const char *format ,…)
so the return type of printf is an int i.e. an interger value,
which is equal to the number of characters printed........
let us prove this by some examples:
(1)
void main()
{
int n;
n=printf("milan");
printf("the returned value from printf is %d:",n);
}

output:
the returned value from printf is:5

Explanation:
 as the number of character in the world "milan" is 5,so the value returned is 5.....

(2)
void main()
{
int a=10;
printf("value returned is:%d",printf("%d",a);
}

output:
10value returned is:2

Explanation:
firstly the inner printf executes and printf the value of a i.e. 10,then it returns the number of chararters printed to the outer printf i.e.2(1 and 0 in 10)...and the outer printf prints the "value returned is:2",where 2 is the value retured by the inner printf.........

(3)
void main()
{
int a=2222;
printf("%d",printf("%d",printf("%d",a)));
}

output:
222241

Explanation:
In this example ,we will really come to that printf returns the actual number of characters printed by it,,firstly the innermost printf prints the value of a i.e 2222 and returns the number of characters printed that is 4,now coming to the second printf ,second printf got the value 4 returned by innermost printf and prints it and return 1(yes,1 not 5 because the value printed by second printf is just 4 not 22224),so the first printf gets the returned value from the second printf which is 1 and print the same...so the output is 222241...in which 2222 is printed by innermost printf,4 is printed by second printf and 1 is printed by the first printf.......