I am not familiar with this type of printf() statement . Why my output look like this ? thanks

Home / Uncategorized / I am not familiar with this type of printf() statement . Why my output look like this ? thanks

Question:
#include
int main()
{
int i=43;
printf("%d",printf("%d",printf("%d",printf("%d",i))));
return 0;
}

what the use of nested printf()? I used value 43 but how other values showing in output?

Output:

43211


Answer:
Consider this as a simpler version of your code:int main(){
int i = 43;
int a = printf("%d", i);
int b = printf("%d", a);
int c = printf("%d", b);
printf("%d", c);
return 0;
}

It’s clear why 43 is printed. As for the other digits, they are the return value of printf:

On success, the total number of characters written is returned.

So, what you should have been getting (considering this was your exact code) is: 43211
43 is the first print
2 is the number of digits printed before (‘4’ + ‘3’)
1 is the number of digits printed before (‘2’)
1 is the number of digits printed before (‘1’)
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *