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

5 comments:

  1. void main(void)
    {
    printf("%d",printf(2+"Akhil"));
    }

    output:
    hil
    3

    ReplyDelete
  2. #include
    void main(void){
    int n;
    printf("\n%d",scanf("%d%d",&n,&n));
    }

    Here, take input as:
    11233
    12111

    the output will be:
    2

    Explanation:
    As we know that both "printf()" and "sacnf()" returns the integer values.
    So here scanf return 2 becose it take two input. And printf prints the integer value which is returned by scanf function.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. output of
    int x
    printf("%d",printf("%d,%d.%d,%d",x,x,x,x));

    it must be=4444 4

    but i am getting 4444 7 why?

    September 12, 2012 12:06 PM

    ReplyDelete
    Replies
    1. Hi harsh,

      I didn't get your doubt???
      what is the value of x????

      Delete

Feel free to comment......